Style 是Window和View的一种外观和格式的属性集合。它可以作为高度,间距,字体大小,背景颜色等属性。Style是一种 xml 资源文件,放在和布局文件不同的文件夹里;
Style 的设计理念和 Web一脉相承——即它们都允许你把内容和样式分离。
例如,你用Style可以把下面这个xml文件:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/hello" /> 替换成下面这种形式:
<TextView
style="@style/CodeFont"
android:text="@string/hello" />你看上面的例子,全部的样式属性都从布局文件里放到一个名为CodeFont的Style里,它是就是一个Style属性。你在接下来的内容里看到这个布局是怎么被定义的。
你可以把Theme当成是整个Activity或者整个程序的样式(Style),它的覆盖范围比上面例子里那个view的范围更大。当一个Style被用作一个Theme时,整个Activity或者整个程序里的每一个View将会用到同样style属性,如果那些属性它们支持的话。举个例子,你可以在一个Activity的样式设置成上面CodeFont,这样的话Activity里面的所有字体都会成绿色的“monospace”。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<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>
</resources> <style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style> 如果你想要继承那些你自己定义的style,你不一定需要用parent这个属性。你可以用另一种方式代替,把你想要继承的那个style作为前缀,用一点把你想要新建的style分隔开来。例如,创建一个继承自上面例子里CodeFont style,只是把颜色设置红的,你可以像下面这样创建一个新的style:<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>你看上面的例子里,在style里并没有parent属性,只是在name属性里把CodeFont作为前缀放在Red的前面,这样也能继承CodeFont里面的所有属性。这个style复写了textColor属性,将字体颜色变成红的。这样定义以后,你就可以用@style/CodeFont.Red 这种方式来引用这个新的style了. <style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style> 像上面这种写法的style就是继承自CodeFont 和 CodeFont.Red,然后加上android:textSize 属性。<EditText
android:inputType="number"
... /> 你也可以创建一个style作为EditText的属性替代上面那种方式:<style name="Numbers"> <item name="android:inputType">number</item> ... </style>这样你的EditText布局文件就可以用用到这个style啦:
<EditText
style="@style/Numbers"
... />这个简单的例子可能会让你觉得用了style反而更麻烦了,其实未必,当你在一个真实的项目里,把很多需要复用到的属性写到一个style里,让所有用到的那些属性的地方使用,那时你就会觉得节省里很多麻烦。<TextView
style="@style/CodeFont"
android:text="@string/hello" /> 现在style CodeFont里的属性将被设置进TextView里。注意:设置style属性时不用加上android: 这个命名空间。<application android:theme="@style/CustomTheme">如果你想要设置一个activity的主题,只要把android:theme属性加到<activity>标签里就好。
<activity android:theme="@android:style/Theme.Dialog">
<activity android:theme="@android:style/Theme.Translucent">如果你喜欢一个主题,但你想调整一些属性,你可以新建一个主题然后用parent属性去继承它。例如,你可以加上自己的颜色在普通的 light 主题上面:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style> (请注意,上面例子里的android:windowBackground属性值只支持已经定义好了的颜色引用,不像android:colorBackground属性值还可以直接用颜色的数值)<activity android:theme="@style/CustomTheme">
<style name="LightThemeSelector" parent="android:Theme.Light">
...
</style><style name="LightThemeSelector" parent="android:Theme.Holo.Light">
...
</style>现在你可以使用任何主题像这个例子一样,你的程序将自动转换到holographic 系列主题,只要你的程序运行在Android 3.0 或以上。原文地址:http://blog.csdn.net/asdzheng/article/details/42838279