android 中style和Theme
2016-01-07 23:20
429 查看
Style:
Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色,将设计和内容分开,便于修改和重复使用。
定义Style:
style文件需要保存在res/values目录下,文件名任意,但是必须是xml文件,sytle文件的根标记必须是<resources>。写了一个简单示例,效果如下:![](http://hi.csdn.net/attachment/201104/21/0_1303352432ql9x.gif)
程序目录结构如下图,其中mystyle.xml是自定义的style文件。
![](http://hi.csdn.net/attachment/201104/21/0_1303352440vApP.gif)
main.xml文件代码:
[xhtml] view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
style="@style/CodeFont"
android:text="测试style">
</TextView>
</LinearLayout>
声明style是CodeFont,对应的是style文件中的style name。mystyle.xml文件中定义了style name是CodeFont:
parent属性表示style之间可以继承,同时可以覆盖parent style的一些属性。
[xhtml] view
plaincopyprint?
<?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的继承:
style继承有两种方式:style的继承可以通过parent属性,用来继承android已经定义好的style,例如:
[xhtml] view
plaincopyprint?
<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
继承了android中的TextAppearance,同时覆盖了android:textColor属性。
如果要继承自定义的style,不需要通过parent属性,只要style的name以需要继承的style的name开始后跟新的style的name,中间用“.”隔开。注意:这种方式只适用与自定义的style继承 。
[xhtml] view
plaincopyprint?
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
新的style继承了CodeFont,则在修改上边例子中的main.xml为:
[xhtml] view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
style="@style/CodeFont.Red"
android:text="测试style">
</TextView>
</LinearLayout>
效果如下,字体颜色变为了红色:
![](http://hi.csdn.net/attachment/201104/21/0_1303352445q75J.gif)
style也可以多级继承:
[xhtml] view
plaincopyprint?
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
字号变大,效果如下:
![](http://hi.csdn.net/attachment/201104/21/0_1303352451oOjC.gif)
sytle的更多属性见android包下的R.attr。需要注意,并不是所有的View都支持定义的style的属性,如果自定义的sytle中包含View不支持的属性,程序会自动忽略它。
Theme:
如果声明一个style作为Theme,需要配置mainfest文件中<activity> 或 <application>的android:theme 属性。
将自定义的style作为application的theme:
修改mystyle.xml为:
[xhtml] view
plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont">
<item name="android:textSize">20sp</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
在mainfest 的application中添加 android:theme属性:
[xhtml] view
plaincopyprint?
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/CodeFont">
则application中的所有text字体都会改变,效果如下:
![](http://hi.csdn.net/attachment/201104/21/0_1303352459BSWL.gif)
在每个<activity>标签中使用android:theme属性:
[xhtml] view
plaincopyprint?
<activity android:name=".MainActivity"
android:label="@string/app_name"
style代码 的地址:http://download.csdn.net/detail/gu821361889/9395195
theme代码的地址:http://download.csdn.net/detail/gu821361889/9395187
相关文章推荐
- JQuery插件Style定制化方法的分析与比较
- 自定义一个theme在不同的sdk环境下继承不同的值
- CSS和JS标签style属性对照表(方便js开发的朋友)
- ppk谈JavaScript style属性
- jQuery UI 应用不同Theme的办法
- 基于android样式与主题(style&theme)的详解
- Android系统自带样式 (android:theme)
- asp中去除html中style,javascript,css代码
- javascript下用for( in )语句 获得所有style 内容的脚本代码
- JavaScript修改css样式style动态改变元素样式
- JavaScript修改css样式style
- Default style sheet for HTML 4
- gridview生成时如何去掉style属性中的border-collapse
- Android入门之Style与Theme用法实例解析
- Android自定义Style实现方法
- JavaScript中的style.display属性操作
- 【转】SourceInsight 中集成Artistic Style 格式化代码 3ff0
- [WP主题]主机主题分享——HostMe
- [WP主题]另类风格的国人优秀WP主题
- Apache - AH01630