Android主题风格(ThemeStyle)介绍
使⽤ Style
Android 中的View、Window等控件通常会定义⼀些属性来表⽰各⾃的外观、格式等信息,例如⼀个TextView中的字体⼤⼩、字体颜⾊,⼀个Dialog的窗⼝类型、窗⼝⼤⼩等。⽽Style就是设置到⼀个View或者Window上的⼀系列属性的集合。我们可以将TextView的字体样式、字体颜⾊、字体⼤⼩等属性定义成⼀个Style,所有使⽤这个Style的TextView将会有相同的字体样式、字体颜⾊、字体⼤⼩。
Style⼀般是以xml⽂件的形式保存在资源⽬录中的,并且是与布局资源layout⽂件分离开来的,这样我们就可以将⼀套Style与使⽤这套Style的控件分离开来,进⾏独⽴维护,⽽且还可以将同⼀套Style复⽤到多个控件上,⽅便移植。从这⼀点上看Style有点类似于⽹页设计中的CSS,允许开发者将设计与内容分离。
下⾯是layout中⼀个TextView的定义,其中的typeface、textColor、textSize⼏个属性是直接定义的:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textColor="#ffff00ff"
android:textSize="24sp"
android:text="@string/hello_world"/>
如果我们将这⼏个属性分离出来,定义成⼀个Style,就会是下⾯的样⼦:
Style:
<style name="CodeFont">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:typeface">monospace</item>
<item name="android:textColor">#ffff00ff</item>
女孩儿英文名<item name="android:textSize">24sp</item>
</style>hated
Layout:
<TextView
android:text="@string/hello_world" />
这样CodeFont这个Style还可以应⽤到其他的TextView,⽽且只需要修改CodeFont,所有使⽤这个Style的TextView都会改变样式。
使⽤ Theme
Theme其实就是应⽤到Activity或者Application上的Style。如上⾯我们刚刚定义的CodeFont,还可以按下⾯的⽅式应⽤到Activity或者Application上。在l中可以如下设置:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
美味英文单词android:label="@string/app_name"
android:theme="@style/CodeFont" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/CodeFont" >
...
<activity>
考研英语一...
</application>
如果Theme应⽤到Activity上,那么这个Activity中所有的View、Window都会使⽤Theme中定义的属性。同样,如果Theme应⽤到Application上,那么这个Application中所有的Activity中的View、Window都会使⽤Theme中定义的属性。
经过上⾯的介绍我们可以知道,Style和Theme的区别就在于应⽤的范围不同。Style是应⽤到单独的⼀个View上的,⽽Theme是应⽤到⼀个Activity或者Application中的View上的。
Style 、Theme 的继承性
Style是可以继承的,⼦Style可以继承⽗Style定义的属性,并重写⽗Style的属性。如下⾯代码,Style CodeFont通过parent 属性继承⾃系统的Style TextAppearance ,并修改了textColor 、typeface 等⼏个属性,⽽其他没有修改的属性默认会使⽤TextAppearance中的定义。⽽如果是想从⾃⼰定义的Style继承,可以不使⽤parent 关键字,⽽是把⽗Style名称做为⼦Style名称的前缀。如下⾯,CodeFont.Red 继承⾃
CodeFont ,并修改了textColor 属性为#FF0000:
使⽤系统Theme
Android系统资源中已经定义了很多的Style和Theme,如Android 4.0之前使⽤的默认Theme,Android 4.0之后引⼊的Holo系列
Theme,Android 5.0之后引⼊的Material系列Theme。我们可以在⾃⼰的应⽤中直接使⽤这些系统内置的Theme或者从这些Theme继承⼦Theme并进⾏修改。
nina mercedez
上⾯介绍的Theme并不是⽀持Android所有的版本,如Holo风格的Theme只⽀持Android 4.0+的版本,Material风格的Theme只⽀持Android 5.0+的版本,那么我们想让我们的应⽤能够⾃⼰根据不同的Android版本适配不同的Theme呢?看下⾯:
<style name ="CodeFont" parent ="@android:style/TextAppearance">
<item name ="android:layout_width">fill_parent </item >
<item name ="android:layout_height">wrap_content </item >
<item name ="android:textColor">#00FF00</item >
<item name ="android:typeface">monospace </item >
</style >
<style name ="CodeFont.Red">
<item name ="android:textColor">#FF0000</item >
</style >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
...
</application>
res/l
<style name="AppTheme"parent="@android:style/Theme">
</style>
res/l
<style name="AppTheme"parent="@android:style/Theme.Holo">
</style>
res/l
emily bear
<style name="AppTheme"parent="@android:style/Theme.Material">
</style>
我们先在默认的资源⽬录res/values/下定义了⼀个AppTheme,继承⾃系统的默认Theme,在Android 4.0的资源⽬录res/values-
v14/下⾯重新定义AppTheme,并继承⾃Theme.Holo,在Android 5.0的资源⽬录res/values-v21/下⾯重新定义AppTheme,并继承
西点师培训
⾃Theme.Material,这样应⽤在Android 4.0之前的版本上运⾏时会使⽤默认Theme,在Android 4.0 ~Android 4.4版本上运⾏时会使⽤Holo主题,在Android 5.0+的版本上会使⽤Material主题。
定义 Style
<declare-styleable name="TextView">
.
..
<!-- Text to display. -->
<attr name="text"format="string"localization="suggested" />
<!-- Hint text to display when the text is empty. -->
<attr name="hint"format="string" />
<!-- Text color. -->
<attr name="textColor" />
...
</declare-styleable>
这⾥为TextView定义了⼀个<declare-styleable>元素,也就是定义了⼀个Style。⾥⾯的每个<attr>为TextView定义了⼀个属性,necktie
如text、hint、textColor等属性,format表⽰该属性的格式,主要有以下⼏种:
盘头化妆学校格式属性说明
reference指向其他资源的引⽤
string字符串资源
integer整数类型的资源
boolean布尔值类型的资源
color颜⾊资源
float浮点型资源
dimension带有单位的数据资源,如14sp,15dp
fraction百分数资源,如20%,30%p
enum 枚举型资源flags 位标记资源
格式属性
说明解析Style
应⽤的编译过程中资源编译⼯具aapt会⾃动创建⼀个R.java ⽂件,⾥⾯会为应⽤中所有的资源、属性定义⼀个ID。编译系统资源framework-res时也会⽣成⼀个R.java ,⾥⾯会创建⼀个styleable 类来存放所有View的属性定义,如下⾯是上⾯看到的TextView ⾥定义的⼏个属性:
out/target/common/obj/APPS/framework-res_intermediates/src/android/R.java
在资源类R.java ⾥text 、hint 、textColor ⼏个属性被定义成了TextView_text 、TextView_hint 、TextView_textColor 等的int型变量。那么这些变量是怎么使⽤的呢? 我们可以看⼀下TextView 构造⽅法⾥的代码: frameworks/ba/core/java/android/widget/TextView.java 这⾥先定义了text、hint两个字符串;通过Theme()得到⼀个Resources.Theme,这就是⼀个主题。;通过theme.obtainStyledAttributes得到⼀个TypedArray,这是⼀个资源属性数组,包含这我们在Theme⾥定义的所有属性的值;通过遍历TypedArray⾥的所有属性,找到TextView_hint、TextView_text属性对应的值,赋值给hint和text;
TextView使⽤text、hint的值。上⾯介绍的是Style和Theme的定义⽅法和使⽤原理,⾄于更深层次的资源解析⽅⾯的问题暂时不做讨论。
public static final class styleable {
...
public static final int TextView_text = 18;
public static final int TextView_hint = 19;
public static final int TextView_textColor = 5;
...}
CharSequence text = "";
CharSequence hint = null ;黄褐斑原因
final Resources.Theme theme = Theme();
TypedArray a = theme.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, defStyleRes);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
...
ca com.android.internal.R.styleable.TextView_hint:
hint = a.getText(attr);
break ;
ca com.android.internal.R.styleable.TextView_text:
text = a.getText(attr);
break ;
...
}
}
tText(text, bufferType);
if (hint != null ) tHint(hint);