在Android中使用主题(Theme)和风格(Style)

主题是由一组应用到整个应用程序中的风格构成的。主题定义了应用程序中的各种外观。android中引入了主题和风格的概念。风格可以被应用到一个视图(View),而主题只能用到整个应用程序。

风格(Styles):

Styles are defined as xml resources files in  directory of your project.

consider this definition of a TextView:

风格(Styles)用xml文件的格式定义,这些文件存放在工程项目中的res/values目录下。

以下是一个TextView的定义:

 
 
Cpp代码   收藏代码
  1. <TextView    android:layout_width="fill_parent"  
  2.      android:layout_height="wrap_content"  
  3.      android:textColor="#FFF"  
  4.      android:typeface="monospace"  
  5.      android:text="First Text View"  
  6.      android:background="#00F"  
  7.      />  
 

以上代码定义了一个TextView,包括文本框的宽度和高度,字体大小,字体格式,并且设置了蓝背景。

如果我们想用风格来实现同样的效果,我们首先要定义一个

this defines a text view with width and height equal to wrap_content, white font color, font type “monospace” and blue back ground.

if we want to have the same results using a style: we first create a xml file (call it styles.xml) inres/values directory and it would be like this:

 
 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3. <style name="BlueLabel">  
  4. <item name="android:layout_width">fill_parent</item>  
  5. <item name="android:layout_height">wrap_content</item>  
  6. <item name="android:typeface">monospace</item>  
  7. <item name="android:background">#00F</item>  
  8. <item name="android:textColor">#FFF</item>  
  9. </style>  
  10. </resources>  
 

then redefine the TextView like this:

 
 
Xml代码   收藏代码
  1. <TextView android:text="First Text View"  
  2.      style="@style/BlueLabel"  
  3.      />  
 

and you will receive the same results.
the attributes in the <item>tag can be any layout property.

Inheriting Styles:

Styles in Android has an interesting feature which is the ability to inherit styles in a fashion similar to that in CSS. consider this example:

we have a style for a button like this:

 
 
Xml代码   收藏代码
  1. <style name="ButtonStyle">  
  2. <item name="android:layout_width">wrap_content</item>  
  3. <item name="android:layout_height">wrap_content</item>  
  4. <item name="android:textSize">15px</item>  
  5. <item name="android:typeface">serif</item>  
  6. </style>  
 

the button will appear like this:

we can make this style inherit from BlueLabel defined previously by adding the parent attribute to the <style*gt; tag:

 
 
Xml代码   收藏代码
  1. <style name="ButtonStyle" parent="BlueLabel">  
 

then the button will be like this:


the button inherited the background color from the parent style.

another interesting feature in styles inheritance is the ability to inherit from the platform built-in styles defined in the android.R.style namespace. to know more about the platform styles check this link

or you can type in your editor (Eclipse) android.R.style and let the intelli-sense list you the complete list of platform styles, if you want to use them in your xml just replace the undrscores with a period like this:
Widget_Button becomes @android:style/Widget.Button.

In the previous button style example we will set the parent of the style to be@android:style/Widget.Button.Small
and the button will be like this:


Note: if you apply a style to a ViewGroup widget, it’s child widgets will not inherit that style.

Using Themes:

you can apply styles as themes on an activity level or application level.

if you apply a theme on an activity level then all widgets within that activity will inherit from that theme.
to do so, open the AndroidManifest.xml and go the <activity> tag and add theandroid:theme attribute:

 
 
Xml代码   收藏代码
  1. <activity android:name=".StylesDemo"  
  2.                   android:label="@string/app_name" android:theme="@style/BlueLabel">  
 

to apply a theme on the application level so that the style will be applied to all activities within your application, open the AndroidManifest.xml and go the <application> tag and add theandroid:theme attribute:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/BlueLabel">

to set the theme of an activity programmatically call this line in the onCreate method

this.setTheme(R.style.BlueLabel);

and that’s was all about using themes and styles in Android, stay tuned for another topic next week.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值