风格和主题(style,themes)

当你设计你的程序的时候,你可以用风格和主题来统一格式化各种屏幕和UI元素。
* 风格( style)是一个包含一种或者多种格式化属性值的集合,你可以将其用为一个单位用在布局XML单个元素当中。比如,你可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。
* 主题( themes)是一个包含一种或者多种格式化属性值的集合,你可以将其为一个单位用在应用中所有的Activity当中或者应用中的某个Activity当中。比如,你可以定义一个主题,它为windowframe和panel的前景和背景定义了一组颜色,并为菜单定义可文字的大小和颜色属性,你可以将这个主题应用在你程序当中所有的Activity里。
<wbr style="line-height:25px">风格和主题的相同点</wbr><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#003366; line-height:25px">风格和主题都是资源。你可以用android提供的一些默认的风格和主题资源,你也可以自定义你自己的主题和风格资源。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">风格和主题的区别</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#003366; line-height:25px">不同的是你通过在AndroidManifest中定义的&lt;application&gt;和&lt;activity&gt;元素将主题添加到整个程序或者某个Activity,<br style="line-height:25px"> 但是主题是不能应用在某一个单独的View里。[然而好像主题的单项可应用在单独的View里]</span><br style="line-height:25px"> 如何新建自定义的风格和主题:<br style="line-height:25px"><span style="color:#000080; line-height:25px">1.在res/values目录下,新建一个xml文件,对于风格文件名为</span><span style="color:#ff00ff; line-height:25px">style.xml</span><span style="color:#000080; line-height:25px">,对于主题文件名为</span><span style="color:#ff00ff; line-height:25px">themes.xml</span><span style="color:#000080; line-height:25px">。增加一个&lt;resources&gt;根节点。<br style="line-height:25px"> 2.对每一个风格和主题,给&lt;style&gt;element增加一个全局唯一的名字,也可以选择增加一个父类属性。在后边我们可以用这个名字来应用风格,而父类属性标识了当前风格是继承于哪个风格。<br style="line-height:25px"> 3.在&lt;style&gt;元素内部,申明一个或者多个&lt;item&gt;,每一个&lt;item&gt;定义了一个名字属性,并且在元素内部定义了这个风格的值。<br style="line-height:25px"> 4.你可以应用在其他XML定义的资源。</span><br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px">风格</span><br style="line-height:25px"> 下边是一个申明风格的实例:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;resources&gt;<br style="line-height:25px"> &lt;stylename="SpecialText"parent="@style/Text"&gt;<br style="line-height:25px"> &lt;itemname="android:textSize"&gt;18sp&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="android:textColor"&gt;#008&lt;/item&gt;<br style="line-height:25px"> &lt;/style&gt;<br style="line-height:25px"> &lt;/resources&gt;</span><br style="line-height:25px"> 如上所示,你可以用&lt;item&gt;元素来为你的风格定义一组格式化的值。在Item当中的名字的属性可以是一个字符串,一个16进制数所表示的颜色或者是其他资源的引用。<br style="line-height:25px"><span style="color:#000080; line-height:25px">注意在&lt;style&gt;元素中的父类属性。这个属性让你可以能够定义一个资源,当前风格可以从这个资源当中继承到值。你可以从任何包含这个风格的资源当中继承此风格。通常上,你的资源应该一直直接或者间接地继承Android的标准风格资源。这样的话,你就只需要定义你想改变的值。</span><br style="line-height:25px"> 在这个例子当中的EditText元素,演示了如何引用一个XML布局文件当中定义的风格:<br style="line-height:25px"> 示例1:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;EditTextid="@+id/text1"<br style="line-height:25px"> style="</span><span style="color:#ff6600; line-height:25px">@style/SpecialText</span><span style="color:#3366ff; line-height:25px">"<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> android:text="Hello,World!"/&gt;</span><br style="line-height:25px"> 现在这个EditText组件的所表现出来的风格就为我们在上边的XML文件中所定义的那样。<br style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">主题</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">就像风格一样,主题依然在&lt;style&gt;元素里边申明,也是以同样的方式引用。<br style="line-height:25px"> 不同的是你通过在AndroidManifest中定义的&lt;application&gt;和&lt;activity&gt;元素将主题添加到整个程序或者某个Activity,</span><br style="line-height:25px"> 但是<span style="line-height:25px"><wbr style="line-height:25px">主题是不能应用在某一个单独的View里</wbr></span><wbr style="line-height:25px">。<br style="line-height:25px"> 下边是申明主题的一个例子:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;resources&gt;<br style="line-height:25px"> &lt;stylename="</span><span style="color:#993300; line-height:25px">CustomTheme</span><span style="color:#3366ff; line-height:25px">"&gt;<br style="line-height:25px"> &lt;itemname="android:windowNoTitle"&gt;true&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="windowFrame"&gt;</span><span style="color:#993300; line-height:25px">@</span><span style="color:#0000ff; line-height:25px">drawable/screen_frame</span><span style="color:#3366ff; line-height:25px">&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="windowBackground"&gt;</span><span style="color:#993300; line-height:25px">@</span><span style="color:#0000ff; line-height:25px">drawable/screen_background_white</span><span style="color:#3366ff; line-height:25px">&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="panelForegroundColor"&gt;#FF000000&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="panelBackgroundColor"&gt;#FFFFFFFF&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="panelTextColor"&gt;</span><span style="color:#993300; line-height:25px">?</span><span style="color:#0000ff; line-height:25px">panelForegroundColor</span><span style="color:#3366ff; line-height:25px">&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="panelTextSize"&gt;14&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="menuItemTextColor"&gt;</span><span style="color:#993300; line-height:25px">?</span><span style="color:#0000ff; line-height:25px">panelTextColor</span><span style="color:#3366ff; line-height:25px">&lt;/item&gt;<br style="line-height:25px"> &lt;itemname="menuItemTextSize"&gt;</span><span style="color:#993300; line-height:25px">?</span><span style="color:#0000ff; line-height:25px">panelTextSize</span><span style="color:#3366ff; line-height:25px">&lt;/item&gt;<br style="line-height:25px"> &lt;/style&gt;<br style="line-height:25px"> &lt;/resources&gt;</span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px">注意我们用了</span><wbr style="line-height:25px"><strong>@</strong><span style="color:#000080; line-height:25px">符号和</span><span style="line-height:25px"><span style="color:#993300; line-height:25px">?</span></span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">符号来应用资源。</span><span style="color:#993300; line-height:25px">@</span><span style="color:#000080; line-height:25px">符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android框架中)。问号</span><span style="color:#993300; line-height:25px">?</span><span style="color:#000080; line-height:25px">表明了我们引用的资源的值在当前的主题当中定义过。通过引用在&lt;item&gt;里边定义的名字可以做到(panelTextColor用的颜色和panelForegroundColor中定义的一样)。这中技巧只能用在XML资源当中。</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">在manifest当中设置主题</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">为了在成用当中所有的Activity当中使用主题,你可以打开AndroidManifest.xml文件,编辑&lt;application&gt;标签,让其包含android:theme属性,值是一个主题的名字,如下:</span><br style="line-height:25px"><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">&lt;applicationandroid:theme="</span><span style="color:#ff6600; line-height:25px">@style/CustomTheme</span><span style="color:#0000ff; line-height:25px">"&gt;</span><br style="line-height:25px"><br style="line-height:25px"> 如果你只是想让你程序当中的某个Activity拥有这个主题,那么你可以修改&lt;activity&gt;标签。<br style="line-height:25px"> Android中提供了几种内置的资源,有好几种主题你可以切换而不用自己写。<br style="line-height:25px"> 比如你可以用对话框主题来让你的Activity看起来像一个对话框。在manifest中定义如下:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">&lt;activityandroid:theme="</span><span style="color:#ff6600; line-height:25px">@android:style/Theme.Dialog</span><span style="color:#0000ff; line-height:25px">"&gt;</span><br style="line-height:25px"> 如果你喜欢一个主题,但是想做一些轻微的改变,你只需要将这个主题添加为父主题。比如我们修改Theme.Dialog主题。<br style="line-height:25px"> 我们来继承Theme.Dialog来生成一个新的主题。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">&lt;stylename="CustomDialogTheme"</span><span style="color:#993300; line-height:25px">parent</span><span style="color:#0000ff; line-height:25px">="</span><span style="color:#ff6600; line-height:25px">@android:style/Theme.Dialog</span><span style="color:#0000ff; line-height:25px">"&gt;</span><br style="line-height:25px"> 继承了Theme.Dialog后,我们可以按照我们的要求来调整主题。我们可以修改在Theme.Dialog中定义的每个item元素的值,然后我们在AndroidManifest文件中使用CustomDialogTheme而不是Theme.Dialog。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">在程序当中设置主题</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">如果需要的话,你可以在Activity当中通过使用方法setTheme()来加载一个主题。</span><br style="line-height:25px"> 注意,如果你这么做的话,你应该初始化任何View之前设置主题。<br style="line-height:25px"> 比如,在调用setContentView(View)和inflate(int,ViewGroup)方法前。<br style="line-height:25px"> 这保证系统将当前主题应用在所有的UI界面。例子如下:<br style="line-height:25px"> protectedvoidonCreate(BundlesavedInstanceState){<br style="line-height:25px"> super.onCreate(savedInstanceState);<br style="line-height:25px"> ...<br style="line-height:25px"> setTheme(android.R.style.Theme_Light);<br style="line-height:25px"> setContentView(R.layout.linear_layout_3);<br style="line-height:25px"> }<br style="line-height:25px"><br style="line-height:25px"> 如果你打算在程序代码中来加载主界面的主题,那么需要注意主题当中不能包括任何系统启动这个Activity所使用的动画,这些动画将在程序启动前显示。<br style="line-height:25px"> 在很多情况下,如果你想将主题应用到你的主界面,在XML中定义似乎是一个更好的办法。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">引用主题属性的属性值</wbr></span><br style="line-height:25px"><span style="color:#003366; line-height:25px">其他种类的资源可以引用当前主题的。这些属性只能是样式资源或XML属性。利用这种方法,您可以定制您的UI界面,使其与当前主题保持风格上的统一。</span><br style="line-height:25px"> 下面的例子展示了如何使用系统主题设置布局器中的文本颜色<br style="line-height:25px"><span style="line-height:25px">示例1</span>:<br style="line-height:25px"><div style="line-height:25px; color:rgb(51,102,255)"> <span style="line-height:25px; white-space:pre"></span>&lt;EditText</div> <div style="line-height:25px; color:rgb(51,102,255)"> android:id="@+id/editText1"</div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(51,102,255); white-space:pre"></span><span style="color:#0000ff; line-height:25px">xmlns:android="http://schemas.android.com/apk/res/android"</span> </div> <div style="line-height:25px; color:rgb(51,102,255)"> <span style="line-height:25px; white-space:pre"></span>android:layout_width="fill_parent" android:layout_height="fill_parent"</div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(51,102,255); white-space:pre"></span><span style="color:#993300; line-height:25px">android:textColor</span><span style="color:#3366ff; line-height:25px">="</span><span style="color:#ff00ff; line-height:25px">?</span><span style="color:#ff9900; line-height:25px">android:textColorSecondary</span><span style="color:#3366ff; line-height:25px">"</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:text="@string/hello" /&gt;</span></div> <span style="line-height:25px">注意</span>:引用主题属性的方法和引用资源的方法很相似,仅仅是用“<span style="color:#993300; line-height:25px">?</span>”替代了“<span style="color:#993300; line-height:25px">@</span>”。当您使用这个标记,你所提供的资源名必须能够在主题属性中找到,因为资源工具认为这个资源属性是被期望得到的。 <div style="line-height:25px"> <span style="color:#993300; line-height:22px">android:textColor</span><span style="color:#3366ff; line-height:22px">="</span><span style="color:#ff00ff; line-height:22px">?</span><span style="color:#ff9900; line-height:22px">android:textColorSecondary</span><span style="color:#3366ff; line-height:22px">"</span><span style="color:#000080; line-height:22px">的准确含义是把控件的</span><span style="line-height:25px; color:rgb(153,51,0)">android:textColor</span><span style="color:#000080; line-height:22px">属性的值设置为</span><span style="color:#ff0000; line-height:22px">当前主题</span><span style="color:#000080; line-height:22px">的</span><span style="line-height:25px; color:rgb(255,153,0)">android:textColorSecondary</span><span style="line-height:25px; color:rgb(0,0,128)">属性的值。</span> </div> <div style="line-height:25px">如果进行完整形式的声明话,其形式就是<span style="color:#ff6600; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">?</span><span style="color:#ff6600; line-height:25px">android:attr/android:textDisabledColor</span>,如<span style="line-height:25px">示例2.</span> <div style="line-height:25px"> <div style="line-height:25px"> <div style="line-height:25px">引用主题属性的其命名语法 和 “<span style="color:#993300; line-height:25px">@</span>” 相似致 :<span style="color:#ff6600; line-height:25px"></span><span style="color:#993300; line-height:25px">?</span><span style="color:#ff6600; line-height:25px">[namespace:]</span><span style="color:#99cc00; line-height:25px">type</span><span style="color:#ff6600; line-height:25px">/name</span>, 因为这里类型<span style="color:#99cc00; line-height:25px">type</span>始终是<span style="color:#99cc00; line-height:25px">attr</span>,所以这里类型<span style="color:#99cc00; line-height:25px">type</span>是可省略的。</div> <div style="line-height:25px">另外<span style="color:#ff6600; line-height:25px">命名空间</span>也可以省略,这时表示的是当前主题,否则表示的是该<span style="color:#000080; line-height:25px">命名空间所对应的应用程序的主题</span>。</div> </div> <div style="line-height:25px"> <div style="line-height:22px"> <span style="line-height:25px">示例2</span>:</div> <div style="line-height:25px"> <div style="line-height:22px"><span style="color:#3366ff; line-height:22px"><span style="line-height:22px; white-space:pre"></span>&lt;EditText</span></div> <div style="line-height:22px"><span style="color:#3366ff; line-height:22px"> android:id="@+id/editText2"</span></div> <div style="line-height:22px"> <span style="line-height:22px; color:rgb(51,102,255); white-space:pre"></span><span style="color:#0000ff; line-height:22px">xmlns:</span><span style="color:#ff6600; line-height:22px">android</span><span style="color:#0000ff; line-height:22px">="http://schemas.android.com/apk/res/android"</span> </div> <div style="line-height:22px"><span style="color:#3366ff; line-height:22px"><span style="line-height:22px; white-space:pre"></span>android:layout_width="fill_parent" android:layout_height="fill_parent"</span></div> <div style="line-height:22px"> <span style="line-height:22px; color:rgb(51,102,255); white-space:pre"></span><span style="color:#993300; line-height:22px">android:textColor</span><span style="color:#3366ff; line-height:22px">="</span><span style="color:#ff00ff; line-height:22px">?</span><span style="color:#ff6600; line-height:22px">android:attr/</span><span style="color:#99cc00; line-height:22px">android:textColorSecondary</span><span style="color:#3366ff; line-height:22px">"</span> </div> <div style="line-height:22px"><span style="color:#3366ff; line-height:22px"><span style="line-height:22px; white-space:pre"></span>android:text="@string/hello" /&gt;</span></div> <div style="line-height:22px"><span style="line-height:22px"><span style="line-height:25px">示例3:</span></span></div> <div style="line-height:25px"> <div style="line-height:25px; color:rgb(51,102,255)"> <span style="line-height:25px; white-space:pre"></span>&lt;EditText</div> <div style="line-height:25px; color:rgb(51,102,255)"> android:id="@+id/editText3"</div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(51,102,255); white-space:pre"></span><span style="color:#0000ff; line-height:25px">xmlns:</span><span style="color:#ff6600; line-height:25px">android2</span><span style="color:#0000ff; line-height:25px">="http://schemas.android.com/apk/res/android"</span> </div> <div style="line-height:25px; color:rgb(51,102,255)"> <span style="line-height:25px; white-space:pre"></span>android:layout_width="fill_parent" android:layout_height="fill_parent"</div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(51,102,255); white-space:pre"></span><span style="color:#993300; line-height:25px">android:textColor</span><span style="line-height:25px; color:rgb(51,102,255)">="</span><span style="color:#ff00ff; line-height:25px">?</span><span style="color:#ff6600; line-height:25px">android2:attr</span><span style="color:#ff00ff; line-height:25px">/</span><span style="color:#99cc00; line-height:25px">android:textColorSecondary</span><span style="line-height:25px; color:rgb(51,102,255)">"</span> </div> <div style="line-height:25px; color:rgb(51,102,255)"> <span style="line-height:25px; white-space:pre"></span>android:text="@string/hello" /&gt;</div> </div> </div> </div> </div> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值