Demo 下载地址 http://download.csdn.net/detail/mwq30123/6781643
相关的Theme,Style,Attr
Theme
Collections of properties for the whole Activityor Application
Definition
OS : frameworks/base/core/res/res/values/themes.xml
APP : res/values/styles.xml
Apply Theme
By XML
Add “android:theme” to <activity> or <Application>element in android manifest
By code
By setTheme() method which mustbe called before setContentView()
Styles
Collections of properties forindividual View or window
Definition
OS :frameworks/base/core/res/res/values/styles.xml
APP : res/values/styles.xml
Apply
By XML
Add style attribute to a Viewelement
Example:
<EditTextid="@+id/editText1
style="@style/TextStyle“
…… />
Attr
Define the attribute’s name and format for theme , style or layoutUsing
Definition
OS : frameworks/base/core/res/res/values/attrs.xml
APP : res/values/attrs.xml
Apply
By XML
<Button
style="?android:attr/buttonStyleSmall“
…… />
Ps: buttonStyleSmall defined in os ’s attrs.xml and used inos’s theme.xml
Example – define button background.
1. Direct Set bg.
android:background="#00ff00“
2. Custom style
<style name="MyBlueButton">
<itemname="android:background">#0000ff</item>
</style>
style="@style/MyBlueButton
3. Custom Theme
• <style name="MyYellowButton"parent="android:Widget.Button">
<item name="android:background">#ffff00</item>
</style>
<style name="MyYellowTheme"parent="android:Theme.Light">
<item name="android:buttonStyle">@style/MyYellowButton</item>
</style>
• <activity
android:name="com.sb.MainActivity"
android:theme="@style/MyYellowTheme" >
4. OS Theme’ s Attr
style="?android:attr/buttonStyleSmall“
5. Custom Attr And CustomView
• in attr.xml
<declare-styleable name="CustomAttr">
<attrname="bgColor" format="reference|color"/>
</declare-styleable>
• in layout.xml
<AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.sb"
……>
<com.sb.CustomButton
……
custom:bgColor="#ff0000"/>
• class CustomButton extends Button
Get the bgColor In CustomButton’s construction and setBackgroundColor(bgColor);
public class CustomButton extends Button{
private Context mContext;
public CustomButton(Context context) {
super(context);
mContext = context;
}
public CustomButton(Context context, AttributeSet attrs){
super(context, attrs);
mContext = context;
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomAttr);
int bgColor = array.getColor(R.styleable.CustomAttr_bgColor,0x000000);
setBackgroundColor(bgColor);
array.recycle();
}
}