Android主题与样式

来源:互联网 发布:表格引用数据并加前缀 编辑:程序博客网 时间:2024/05/01 23:04

一、样式

样式是属性的集合,例如定义属性fontColor、fontSize、layout_width、layout_height等,以独立的资源文件存放在XML文件中,并设置样式的名称。

Android Style类似网页设计中的级联样式CSS设计思路,可以让设计与内容分离,并且可以方便的继承、覆盖、重用。


1.未使用Style

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.       
  8. <TextView  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     android:textColor="#00FF00"  
  12.     android:text="@string/hello" />  
  13.       
  14. <TextView  
  15.     android:layout_width="wrap_content"  
  16.     android:layout_height="wrap_content"  
  17.     android:text="@string/hello" />  
  18.   
  19. <Button   
  20.     android:layout_width="wrap_content"  
  21.     android:layout_height="wrap_content"  
  22.     android:text="@string/hello"  
  23. />  
  24.   
  25. </LinearLayout>  
上述界面中有三个控件,2个TextView、1个Button。他们的布局宽度layout_width与布局高度layout_height都是wrap_content包裹内容。

下面看看,如何使用Style来改进。


2、使用Style

首先,在res/values/下创建Style XML资源文件,这里创建的Style资源文件名命名为styles.xml,这个可以自己自定义。

styles.xml内容如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="wrap_content">  
  4.         <item name="android:layout_width">wrap_content</item>  
  5.         <item name="android:layout_height">wrap_content</item>  
  6.     </style>  
  7. </resources>  
其中,style标签中name属性类似CSS中的class name,item标签中的name对应属性的名字,item标签对中的text对应属性的值。


使用样式:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.       
  8. <TextView  
  9.     style="@style/wrap_content"  
  10.     android:textColor="#00FF00"  
  11.     android:text="@string/hello" />  
  12.       
  13. <TextView  
  14.     style="@style/wrap_content"  
  15.     android:text="@string/hello" />  
  16.   
  17. <Button   
  18.     style="@style/wrap_content"  
  19.     android:text="@string/hello"  
  20. />  
  21.   
  22. </LinearLayout>  


3、样式的继承

有两种方式来实现继承,一是通过style的parent属性,二是使用类似CSS中的命名规则来实现。

一、通过parent属性

修改styles.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="wrap_content">  
  4.         <item name="android:layout_width">wrap_content</item>  
  5.         <item name="android:layout_height">wrap_content</item>  
  6.     </style>  
  7.       
  8.     <style name="inherit" parent="wrap_content">  
  9.         <item name="android:textColor">#00FF00</item>  
  10.     </style>  
  11. </resources>  
新增名为inherit的样式,并且继承名为wrap_content样式,也就是说inherit具有wrap_content样式中定义的属性参数。

引用方式:style="@style/inherit"


二、通过命名规则实现

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="wrap_content">  
  4.         <item name="android:layout_width">wrap_content</item>  
  5.         <item name="android:layout_height">wrap_content</item>  
  6.     </style>  
  7.       
  8.     <style name="wrap_content.inherit">  
  9.         <item name="android:textColor">#00FF00</item>  
  10.     </style>  
  11. </resources>  
通过“.”号实现继承。

引用方式:style="@style/wrap_content.inherit"


二、主题

针对应用中所有Activity或者针对某个Activity设置样式,可以通过编辑AndroidManifest.xml来完成。

1.设置应用中所有Activity活动的主题

[html] view plaincopy
  1. <application android:theme="@style/wrap_content">  
这样,应用中所有Activity中的所有组件都会默认使用包裹布局。


2.设置某个指定的Activity主题

[java] view plaincopy
  1. <activity android:theme="@style/wrap_content">  

另外,android提供了许多自带的主题样式。例如Theme.Dialog、Theme.Translucent等等。使用方式也很简单

[html] view plaincopy
  1. <activity android:theme="@android:style/Theme.Dialog">  



样式属性参考:http://android.toolib.net/reference/android/R.attr.html

主题属性参数:http://android.toolib.net/reference/android/R.styleable.html#Theme

样式与主题参考:http://android.toolib.net/guide/topics/ui/themes.html

0 0