Android UI–Style 和 Theme

来源:互联网 发布:阿里云win404 编辑:程序博客网 时间:2024/05/16 05:03

Android UI–Style 和 Theme


1 style和theme概述


style是一系列的属性的集合,目的是定义view和window的外观。

style来源于web设计下的CSS。

theme是这样的一种style,她是针对activity和application,theme的定义作用于activity或application下的所有view。

2 定义style

一个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>

 

2.1 style继承

parent可以是用户自定义的style,也可以是android系统的style

如果是继承用户自定义的style,可以不用直接定义parent属性,而是在定义style的name时增加前缀即可

举个例子:

a 这个style继承了(CodeFont)style

   <style name="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
b 这个例子继承了(CodeFont.Red)style
<style name="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>

2.2 style的属性

在style中可以定义哪些属性呢?

a 定义针对一个view的style,可以看这个view支持的XML属性

b 有些属性对于view是没有意义的,只可以用来定义theme,可以参考android.R.attr类中window开头的属性

 

3 如何应用style和theme

3.1 应用于View

例子

<TextViewstyle="@style/CodeFont" android:text="@string/hello" />

3.2 应用主题于app和activity

例子

<application android:theme="@style/CustomTheme">

3.3 基于不同版本的平台使用不同的style

举个例子

默认情况:res/values/styles.xml

<style name="LightThemeSelector" parent="android:Theme.Light">    ...</style>
平台版本是11的情况:
res/values-v11/styles.xml
<style name="LightThemeSelector" parent="android:Theme.Holo.Light">    ...</style>

4 如何使用android自定义style和theme

android.R.style累定义了所有系统自带的style,可以通过看系统自带的style和theme的源码,学习更多东西。




0 0
原创粉丝点击