API Guide--UI Interface--Styles and Themes
来源:互联网 发布:大文豪曼因斯坦知乎 编辑:程序博客网 时间:2024/05/20 00:36
原文链接:http://developer.android.com/guide/topics/ui/themes.html
============================Style and Theme=================================
style是一些指定view和window格式和外观的属性的集合。style是指能指定类似于height、padding、fontcolor、fontSize、backgroundcolor等的属性。style是定义在resource中的XML,不同于指定layout的xml。android中的style跟网页设计中的stylesheet差不多。他们允许你将设计和内容分离。
例如:通过style你可以把下面的layout 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" />
所有在layout XML中与属性都已经移到一个叫做CodeFont的集合中,并且冠以一个style属性,你将会在下面部分看到
如何定义style属性。
+++++++++++++++++++++++++DefineStyle++++++++++++++++++++++++++++++++++++++++++++++
要创建一系列style,需要把他们存放在你工程的res/values目录下。XML的名字可以自定,但是一定要是XML后缀并且存在
res/values目录下。
这个XML的根节点必须是<resources>
每一个style,要用一个<style>标签加name(必需属性)唯一标识这个style,然后为每个属性加<item>标签,也要加name(必需)属性来指定,item的值可以是”关键字“,”颜色值“,”其他资源的引用“,或者是根据style属性而定的其他值。例如:
<?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>
每一个<resources>的子元素,都会在编译的时候转换成一种app资源。这些资源可以以<style>的name值来引用,上面的例子就是用@style/CodeFont被引用的。
在<style>中的parent标签指定了要继承的父类style,你可以按照你的意愿复写其中的方法
需要注意的是,用于activity和application的style和用于View的完全一样。上面定义的XML,可以以style的形式适用于View,也可以以theme的形式适用于整个activity和application。如何为一个view部署style,或者为activity部署theme,我们以后讲
+++++++++++++++++++++++++Inheritance+++++++++++++++++++++++++++++++++++++++++++++
在<style>中的parent属性,让你指定你可以从哪个父属性中继承的属性。你这样可以从以后的属性继承属性,并且添加你想要定义的属性。你也可以继承你自己以前创建的属性。例如:
<style name="GreenText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
如果你想继承你已经创建的属性,你不需要使用parent元素。你可以把被继承的属性加到子属性的前面。用.隔开。例如:
<style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
要注意上面,<style>标签没有parent属性。
你可以继续这样继承下去无数次,例如:
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
+++++++++++++++++++++++++Style property+++++++++++++++++++++++++++++++++++++++++++++
现在你了解了style是怎么定义的了,你还要了解你定义的<item>元素中能接受哪些属性。你可能已经对一些属性已经很熟悉了。例如layout_width ,textColor。还有更多的属性你可以使用。
具体哪一种view可以使用哪些属性,你可以到对应的类文档里查找,哪里列出了所有可支持的XML属性。
每个View支持的style属性不同,如果定义中有view不支持的属性,他会自动忽略
注意:每个<itme>元素中的name标签,要以android:开头 ,例如:<item name="android:inputType">
+++++++++++++++++++++++++Applying Styles and Themes to your UI+++++++++++++++++++++++++++++++++++++++++++++
有两种设置style的方式:
1,对于单个view,在layout的xml文件中加入style属性
2,对于整个Activity或者Application,在AndroidMenifist文件中的<application>或者<activity>元素中添加android:theme标签
当你仅仅对一个view使用了style,style仅仅对一个view起作用:如果你对一个ViewGroup使用了style,子View不会继承这些style属性。
当你对一个Activity或者Appliction使用了style,每个view都会继承到他们支持的属性,不继承的属性将会被忽略
+++++++++++++++++++++++++Applying style to a View+++++++++++++++++++++++++++++++++++++++++++++
例子:
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
+++++++++++++++++++++++++Apply a theme to an Activity or application+++++++++++++++++++++++++++++++++++++++++++++
例子:
<application android:theme="@style/CustomTheme">
android内置了好多属性,例如:
你想要activity不完全盖住
<activity android:theme="@android:style/Theme.Dialog">
你想背景透明:
<activity android:theme="@android:style/Theme.Translucent">
如果你想改变一个现有的属性
<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,他可以直接给一个特定的值
+++++++++++++++++++++++++Select a theme based on platform version+++++++++++++++++++++++++++++++++++++++++++++
不同版本的android系统,可以通过改变res/values文件夹的来获取各个版本系统支持的内置属性。例如res/values-v11就可以让你的程序
在3.0以上系统使用它的内置属性,前提res/values中有相同的属性,他才会自动选择
+++++++++++++++++++++++++Using Platform Styles and Themes+++++++++++++++++++++++++++++++++++++++++++++
源码地址
Android Styles (styles.xml) :https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml
Android Themes (themes.xml) :https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml
0 0
- API Guide--UI Interface--Styles and Themes
- API/Guides/User Interface/Styles and Themes 翻译
- Android API Guides---Styles and Themes
- Android 开发指南翻译开篇--User Interface -Styles and Themes
- Android官方文档之User Interface(Styles and Themes)
- Applying styles and themes
- Android Styles and Themes
- Styles and Themes
- Styles and Themes
- android--Styles and Themes
- Android - Styles and Themes
- Styles and Themes
- styles and themes
- Android-Styles and Themes
- android-Styles and Themes
- Styles and Themes
- Styles and Themes
- Applying themes and styles(3)_Applying themes and styles
- Python编程——IO(输入输出)
- awg线规,直径,面积,电流对照
- 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别
- 看看baidu是如何AJAX跨域的
- PHP SPL笔记
- API Guide--UI Interface--Styles and Themes
- eclipse快捷键
- python 调用mysql存储过程返回结果集
- 51 IAP说明
- 微派(V-TOP)第五届企业微信营销培训取得圆满成功
- Android–基于PUSH服务的位置共享软件app的实现
- 在Linux下使用命令行安装DB2
- SQL Server中的Merge关键字
- 不重启vim就让vimrc文件生效