一、Style用来定义Android View或者Windows样式,在res/values/styles.xml文件中进行定义,名字唯一;
</pre><pre name="code" class="html"><pre class="prettyprint" style="font-size: 13px; margin-top: 0px; margin-bottom: 1em; color: rgb(0, 102, 0); font-stretch: normal; line-height: 1.5; padding: 1em; 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);"><resources></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="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);">"CodeFont"</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);">"@android:style/TextAppearance.Medium"</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><span class="pln" style="color: rgb(0, 0, 0);">item name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"android:layout_width"</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);">fill_parent</span><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="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);">item name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"android:layout_height"</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);">wrap_content</span><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="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);">item name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"android:textColor"</span><span class="pun" style="color: rgb(102, 102, 0);">>#</span><span class="lit" style="color: rgb(0, 102, 102);">00FF00</span><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="pun" style="color: rgb(102, 102, 0);"><</span><span class="pln" style="color: rgb(0, 0, 0);">item name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"android:typeface"</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="pln" style="color: rgb(0, 0, 0);">monospace</span><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>
使用在:
<pre class="prettyprint" style="font-size: 13px; margin-top: 0px; margin-bottom: 1em; color: rgb(0, 102, 0); font-stretch: normal; line-height: 1.5; padding: 1em; overflow: auto; border: 1px solid rgb(221, 221, 221); background: rgb(247, 247, 247);"><span class="tag" style="color: rgb(0, 0, 136);"><TextView</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="atn" style="color: rgb(136, 34, 136);">style</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(136, 0, 0);">"</span><span class="pun" style="color: rgb(102, 102, 0);">@</span><span class="pln" style="color: rgb(0, 0, 0);">style</span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln" style="color: rgb(0, 0, 0);">CodeFont</span><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);">android:text</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(136, 0, 0);">"@string/hello"</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span>
等价于:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello" />
二、使用方式
直接通过View,如TextView的style属性进行指定,@style/开头表示自定义的style,@andriod/style开头表示是系统为我们提供的;在AndroidMainfest.xml中进行使用时,在<application>中通过android:Theme进行指定,则将应用到所有的Activity上面,在<activiy>中指定,则只应用在一个Activity上面。
三、继承
1、继承自定义的style可以通过如下方式继承:
</pre><pre class="prettyprint" style="font-size: 13px; margin-top: 0px; margin-bottom: 1em; color: rgb(0, 102, 0); font-stretch: normal; line-height: 1.5; padding: 1em; overflow: auto; border: 1px solid rgb(221, 221, 221); background: rgb(247, 247, 247);"><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="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);">"CodeFont.Red"</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><span class="pln" style="color: rgb(0, 0, 0);">item name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"android:textColor"</span><span class="pun" style="color: rgb(102, 102, 0);">>#</span><span class="pln" style="color: rgb(0, 0, 0);">FF0000</span><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>
<pre class="prettyprint" style="font-size: 13px; margin-top: 0px; margin-bottom: 1em; color: rgb(0, 102, 0); font-stretch: normal; line-height: 1.5; padding: 1em; overflow: auto; border: 1px solid rgb(221, 221, 221); background: rgb(247, 247, 247);"><span class="tag" style="color: rgb(0, 0, 136);"><style</span><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);">"CodeFont.Red.Big"</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><span class="pln" style="color: rgb(0, 0, 0);">item name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(136, 0, 0);">"android:textSize"</span><span class="pun" style="color: rgb(102, 102, 0);">></span><span class="lit" style="color: rgb(0, 102, 102);">30sp</span><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>
2、继承系统的style只能通过 <style>标签中的parent属性指定父style;
四、针对不同的版本定义不同的布局
默认的styles.xml文件实在res/values中,而res/values-v11下的styles.xml则指定了当api level为11对应的Android 3.0平台,只要在这些styles.xml定义相同的名字的style,那么系统就会自动到相应版本的文件夹下,加载相应的style了。
如:在res/values/styles.xml中定义
<pre class="prettyprint" style="font-size: 13px; margin-top: 0px; margin-bottom: 1em; color: rgb(0, 102, 0); font-stretch: normal; line-height: 1.5; padding: 1em; overflow: auto; border: 1px solid rgb(221, 221, 221); background: rgb(247, 247, 247);"><span class="tag" style="color: rgb(0, 0, 136);"><style</span><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);">"LightThemeSelector"</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);">"android:Theme.Light"</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><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);"></style></span>
在res/values-v11/styles.xml中定义
<pre class="prettyprint" style="font-size: 13px; margin-top: 0px; margin-bottom: 1em; color: rgb(0, 102, 0); font-stretch: normal; line-height: 1.5; padding: 1em; overflow: auto; border: 1px solid rgb(221, 221, 221); background: rgb(247, 247, 247);"><span class="tag" style="color: rgb(0, 0, 136);"><style</span><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);">"LightThemeSelector"</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);">"android:Theme.Holo.Light"</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><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="tag" style="color: rgb(0, 0, 136);"></style></span>
五、
Theme:style应用到application或者activity上就是了。
以上来自Android开发文档。
时间: 2024-10-14 14:30:03