android style(样式)和theme(主题)设置
1、样式定义
android的样式定义在res/values/style.xml文件中,类似web前端中将样式定义在某个css文件中,但android的style.xml是自动加载的,不需要手动import或link。目前还不了解android是否可以或怎么定义多个style文件。
如下是一组样式的定义
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><!--全局字体样式-->
- <stylename="DefaultFontStyle">
- <itemname="android:textSize">18px</item>
- <itemname="android:textColor">#0000CC</item>
- </style>
- <!--全局背景色-->
- <stylename="DefaultBgColor"parent="@style/DefaultFontStyle">
- <itemname="android:background">#F2F2F2</item>
- </style>
- <!--全局样式-->
- <stylename="DefaultStyle"parent="@style/DefaultBgColor">
- </style></span>
a. android的样式定义是通过style标签完成的,通过添加item元素设置不同的属性值
b.样式可以通过设置parent进行继承。上面的DefaultBgColor继承自DefaultFontStyle,而DefaultStyle又继承自DefaultBgColor,这样DefaultStyle就有了字体大小颜色、背景色的属性了。
c. android的主题样式和一般样式的定义是一样的,只是引用时不同,下面将会介绍
2、单个view如何设置样式
比如TextView,设置样式如下
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="我在做什么:"
- android:textSize="18px"
- android:textColor="#0000CC"
- /></span>
也可以引用第一部分定义的样式,如下
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="我在做什么:"
- style="@style/DefaultStyle"
- /></span>
设置view的style属性进行样式调用,推荐使用此种方式将样式和布局分离。其他view及viewGroup设置相同。
对于单个view的更多属性可以参考http://developer.android.com/reference/android/R.styleable.html#View
或具体的某个view的sdk文档xml attribute.
3、全局样式设置
在web前端编程中,可以使用
[html] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);">body{
- background:#cce8cf;
- color:#000;
- font-family:宋体verdana,tahoma;
- font-size:18px;
- padding:1px2px02px;
- counter-reset:section;
- }</span>
设置全局的样式
[html] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);">div{
- margin-top:10px;
- margin-bottom:10px;
- }</span>
设置单个标签的样式
android中我们同样可以办到,只是这种全局样式被称作主题theme,比如对于整个应用默认字体都要18px,颜色为#0000CC,背景色为#F2F2F2,我们可以通过在AndroidManifest.xml设置application的android:theme属性完成,如下:
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><applicationandroid:theme="@style/DefaultStyle"></span>
DefaultStyle即为第一部分中定义的主题,在第一部分中我们提到的主题和样式定义一样也是这个意思,只是引用的时候使用android:theme罢了。
下面为单个activity设置主题的代码
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><activityandroid:name=".AccountManageActivity"
- android:theme="@style/DefaultStyle"></span>
activity的主题还有一些特殊设置,如
[xml] view plain copy print ?
- <spanstyle="background-color:rgb(255,255,255);">android:theme="@android:style/Theme.Dialog"</span>
为对话框样式设置
主题的设置也可以在代码中通过setTheme(R.id.xx)完成。
接下来问题就出现了,如果一个应用设置了application的主题,设置了activity,设置了view的样式,那么view的各个样式属性值究竟是多少呢??
3、样式继承关系
android的样式采取和css中一样的覆盖、继承原则,和面向对象的子类覆盖父类属性、继承没有定义的父类属性值的原则是一样的。
如果一个TextView自己设置了样式,它的ViewGroup设置了样式,activity设置了主题,application设置了主题。
它会先读取自己样式的值,对于自己没有的样式向上查找第一个找到的值即为要采取的值。
依次读取的顺序为View自己的样式->上一层ViewGroup的属性值->上上层ViewGroup的属性值->…->activity主题->activity主题。
例子如下
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><!--全局字体样式-->
- <stylename="DefaultFontStyle">
- <itemname="android:textSize">18px</item>
- <itemname="android:textColor">#0000CC</item>
- </style>
- <!--全局背景色-->
- <stylename="DefaultBgColor"parent="@style/DefaultFontStyle">
- <itemname="android:background">#F2F2F2</item>
- </style>
- <!--全局样式-->
- <stylename="DefaultStyle"parent="@style/DefaultBgColor">
- </style>
- <!--textView字体样式-->
- <stylename="TextViewFontStyle">
- <itemname="android:textSize">20px</item>
- </style></span>
application主题为
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><applicationandroid:theme="@style/DefaultStyle"></span>
activity主题为
[xml] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><activityandroid:name=".AccountManageActivity"
- android:theme="@style/DefaultStyle"></span>
textView样式设置如下
[java] view plain copy print ?- <spanstyle="background-color:rgb(255,255,255);"><TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="我在做什么:"
- style="@style/TextViewFontStyle"
- /></span>
则textView中最终字体大小为20px,颜色采用activity中设置的0000CC
更多相关文章
- 填坑总结:通过selector的android:state_checkable和android:state
- android与html5的交互——数据库操作,UI操作,以及html5的localStor
- android notification通知栏及8.0适配
- Android(安卓)各版本 设置 USB 默认连接 MTP 模式 ( Android(安
- android优化 清除无效代码 UCDetector
- Android_TextView属性XML详解
- 基于android的远程视频监控系统(已开放源码)
- Android的Window类
- Android中主题与样式