原文地址:Style Resource
原文地址:Styles and Themes
简介:
一个style resource为一个UI定义一个format和look。它可以用在自定义View中(from within a layout file),或者是整个Activity和应用(from within the manifest file)。它可以指定特定的性能例如height、padding、font color、font size、background color等。它在web设计中的原理是层叠样式表,它让你在设计的过程中与content分离。
For example, by using a style, you can take this layout XML:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello" />
And turn it into this:
<TextView style="@style/CodeFont" android:text="@string/hello" />注意点一:一个style仅仅是一个简单的resource,这个resource使用name属性中提供的值,而不是XML file文件文件名。同时还可以将style resources结合其他的resources在同一个XML file中,都放在同一个<resources>元素下。
Theme:
主题theme就是一种应用在整个Activity或application的style。
最简单的使用方法:
文件存放地址:res/values/filename.xml
注意点一:这里filename可以随意取,但是要引用style时用的是它内部的元素的name属性作为资源ID。
引用方法:In XML: @[package:]style/style_name
语法:
<pre class="stx prettyprint" style="margin-top: 10px; margin-bottom: 1em; padding: 1em; font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 1.5; overflow: auto; border: 1px solid rgb(221, 221, 221); background: rgb(247, 247, 247);"><span class="pun" style="color: rgb(102, 102, 0);"><?</span><span class="pln" style="color: rgb(0, 0, 0);">xml version</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"1.0"</span><span class="pln" style="color: rgb(0, 0, 0);"> encoding</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"utf-8"</span><span class="pun" style="color: rgb(102, 102, 0);">?></span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="tag" style="color: rgb(0, 0, 136);"><</span><a target=_blank href="http://wear.techbrood.com/guide/topics/resources/style-resource.html#resources-element" style="color: rgb(37, 138, 175); text-decoration: none;"><span class="tag" style="color: rgb(0, 0, 136);">resources</span></a><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="tag" style="color: rgb(0, 0, 136);"><</span><a target=_blank href="http://wear.techbrood.com/guide/topics/resources/style-resource.html#style-element" style="color: rgb(37, 138, 175); text-decoration: none;"><span class="tag" style="color: rgb(0, 0, 136);">style</span></a><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="atn" style="color: rgb(136, 34, 136);">name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(136, 0, 0);">"</span><em><span class="atv" style="color: rgb(136, 0, 0);">style_name</span></em><span class="atv" style="color: rgb(136, 0, 0);">"</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="atn" style="color: rgb(136, 34, 136);">parent</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(136, 0, 0);">"@[package:]style/</span><em><span class="atv" style="color: rgb(136, 0, 0);">style_to_inherit</span></em><span class="atv" style="color: rgb(136, 0, 0);">"</span><span class="tag" style="color: rgb(0, 0, 136);">></span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);"><</span><a target=_blank href="http://wear.techbrood.com/guide/topics/resources/style-resource.html#item-element" style="color: rgb(37, 138, 175); text-decoration: none;"><span class="pln" style="color: rgb(0, 0, 0);">item</span></a><span class="pln" style="color: rgb(0, 0, 0);">
name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"</span><em><span class="str" style="color: rgb(136, 0, 0);">[package:]style_property_name</span></em><span class="str" style="color: rgb(136, 0, 0);">"</span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="pun" style="color: rgb(102, 102, 0);">></span><em><span class="pln" style="color: rgb(0, 0, 0);">style_value</span></em><span class="pun" style="color: rgb(102, 102, 0);"></</span><span class="pln" style="color: rgb(0, 0, 0);">item</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="tag" style="color: rgb(0, 0, 136);"></style></span><span class="pln" style="color: rgb(0, 0, 0);">
</span><span class="tag" style="color: rgb(0, 0, 136);"></resources></span>
其中:
<resources>:
作为根节点,没有任何属性。
<style>:
作为一个单独的style。包含<item>元素。
它有两个属性:
name:String. Required. 它是style的name,作为这个style的资源ID用来引用。
parent:指的是这个style可以继承的那个父类style,可以不要这个属性。继承后也可以在这里覆盖父类的一些属性值。
作为父类可以是自定义的也可以是安卓系统自带的。,比如说系统默认的text appearance:
<style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style>还可以这样用:如果你已经自定义了CodeFoint这个style,现在你想继承它,可以不使用parent属性,而是在style的name属性名称前加上父类前缀。引用方法:
@style/CodeFont.Red
.
<style name="CodeFont.Red"> <item name="android:textColor">#FF0000</item> </style>当然还可以多级继承:
<style name="CodeFont.Red.Big"> <item name="android:textSize">30sp</item> </style>注意点一:这种省略parent继承只适用于自定义资源。
<item>:
为style定义它的性能,这个性能是夹在<item>和</item>之间的。<item>元素一定是放在<style>中的。
它有一个属性:name:
Attribute resource. Required。是夹在<item>和</item>之间的性能的名称,如果需要的话需要将包名作为前缀,(for example android:textColor
)。
举例1:
XML file for the style (saved inres/values/
):
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomText" parent="@style/Text"> <item name="android:textSize">20sp</item> <item name="android:textColor">#008</item> </style> </resources>
TextView
(saved in
res/layout/
):
<?xml version="1.0" encoding="utf-8"?> <EditText style="@style/CustomText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" />
举例2:
<?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 Properties
下面来看看什么类型的style properties可以被定义在<item>元素中。
对于特定的View,可用的Properties可以去相应的类文件中去查询,里面列出了所有支持的XML属性。比如说去TextView的TextView XML attributes表中去查询。
例如:
<EditText android:inputType="number" ... />
You can instead create a style for the EditText
element that includes this property:
<style name="Numbers"> <item name="android:inputType">number</item> ... </style>
So your XML for the layout can now implement this style:
<EditText style="@style/Numbers" ... />但是当style变得复杂的时候就比较麻烦了。
我们可以在R.attr在查询所有可用的属性。但是不是所有View都能支持所有的属性的。不过当你给一个View应用的style中包含这个View不支持的属性,那么View只会挑选它支持的属性,把那些不支持的忽略掉。
还有一些属性只支持theme但是不支持任何View。比如说application title的隐藏,状态栏的隐藏,改变window的背景等。这些特殊的theme属性可以去R.attr下的window中查询,例如:windowNoTitle
and windowBackground。
注意点二:不要放弃在每个属性名称前加android:
namespace。For example: <item name="android:inputType">
.
Applying Styles and Themes to the UI
使用style的两种方式:
第一种是在单个的View中在layout中通过style属性添加。
第二种是在Manifest文件中为<activity>或<application>添加android:theme
属性。
注意点一:如果将一个style应用在ViewGroup上,那子View是不会继承这个style的属性的。但你可以通过把style做成一个theme就可以将它设置在所有的View上。
注意点二:style属性的前面不需要加android:
namespace前缀。
将一个自定义theme应用到Activity或application上:
<application android:theme="@style/CustomTheme">
<activity android:theme="@style/CustomTheme">将一个系统自带theme应用到Activity或application上:
<activity android:theme="@android:style/Theme.Dialog">
<activity android:theme="@android:style/Theme.Translucent">如果你想修改一个已存在的theme,只需要添加parent就好了:
<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>
<activity android:theme="@style/CustomTheme">
注意点一:color需要作为一个分开的resource,因为android:windowBackground属性仅仅支持另一个资源的引用,不像
android:colorBackground就可以直接给一个颜色值。
根据平台版本选择对应的主题
举例:
在旧版本上:
<style name="LightThemeSelector" parent="android:Theme.Light"> ... </style>到了新版本(比如说Android3.0 API Level11)上的时候: you can place an alternative declaration for the theme in an XML file in
res/values-v11
, but make the parent theme the holographic theme:
<style name="LightThemeSelector" parent="android:Theme.Holo.Light"> ... </style>
使用系统自带的Styles和Themes
在R.style中可以找到所有可用的styles。这样用:
For example, you can apply the Theme_NoTitleBar
theme with"@android:style/Theme.NoTitleBar"
.
在R.style中对各种style的描述可以很粗略,所以你还是需要去阅读源码去了解这个style提供的性能:
就比如说你可以找到<style name="Theme.Dialog">
的声明。