Android中Style和Theme的使用

原文地址::http://blog.csdn.net/wuxianglong/article/details/6337945

相关网帖::

1. Android用style简化layout文件----http://mycoding.iteye.com/blog/966726

2.Android开发之:如何使用样式和主题----http://www.ideasandroid.com/archives/322

 

Android平台的样式和主题

样式见:http://www.ideasandroid.com/android/sdk/styles.xml
主题见:http://www.ideasandroid.com/android/sdk/themes.xml

 

 

 

 

Style:

Style是View中一些属性的集合,包括height,padding,font color,background等等,Style单独定义在xml文件中,类似与web页面中css的角色,将设计和内容分开,便于修改和重复使用。

 

定义Style:

style文件需要保存在res/values目录下,文件名任意,但是必须是xml文件,sytle文件的根标记必须是<resources>。写了一个简单示例,效果如下:

程序目录结构如下图,其中mystyle.xml是自定义的style文件。

 

main.xml文件代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <TextView  
  7.         style="@style/CodeFont"  
  8.         android:text="测试style">  
  9.     </TextView>  
  10. </LinearLayout>  

声明style是CodeFont,对应的是style文件中的style name。mystyle.xml文件中定义了style name是CodeFont:

parent属性表示style之间可以继承,同时可以覆盖parent style的一些属性。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="CodeFont" parent="@android:style/TextAppearance.Medium">  
  4.         <item name="android:layout_width">fill_parent</item>  
  5.         <item name="android:layout_height">wrap_content</item>  
  6.         <item name="android:textColor">#00FF00</item>  
  7.         <item name="android:typeface">monospace</item>  
  8.     </style>  
  9. </resources>  

 

Style的继承:

style继承有两种方式:

  • style的继承可以通过parent属性,用来继承android已经定义好的style,例如:

  1. <style name="GreenText" parent="@android:style/TextAppearance">  
  2.     <item name="android:textColor">#00FF00</item>  
  3. </style>  

继承了android中的TextAppearance,同时覆盖了android:textColor属性。

  • 如果要继承自定义的style,不需要通过parent属性,只要style的name以需要继承的style的name开始后跟新的style的name,中间用“.”隔开。注意:这种方式只适用与自定义的style继承

  1. <style name="CodeFont.Red">  
  2.     <item name="android:textColor">#FF0000</item>  
  3. </style>  

新的style继承了CodeFont,则在修改上边例子中的main.xml为:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <TextView  
  7.         style="@style/CodeFont.Red"  
  8.         android:text="测试style">  
  9.     </TextView>  
  10. </LinearLayout>  

效果如下,字体颜色变为了红色:

style也可以多级继承:

  1. <style name="CodeFont.Red.Big">  
  2.     <item name="android:textSize">30sp</item>  
  3. </style>  

字号变大,效果如下:

sytle的更多属性见android包下的R.attr。需要注意,并不是所有的View都支持定义的style的属性,如果自定义的sytle中包含View不支持的属性,程序会自动忽略它。

 

 

Theme:

 

如果声明一个style作为Theme,需要配置mainfest文件中<activity> 或 <application>的android:theme 属性。

将自定义的style作为application的theme:

修改mystyle.xml为:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="CodeFont">  
  4.         <item name="android:textSize">20sp</item>  
  5.         <item name="android:typeface">monospace</item>  
  6.     </style>  
  7. </resources>  

 

在mainfest 的application中添加 android:theme属性:

  1. <application android:icon="@drawable/icon"  
  2.       android:label="@string/app_name"  
  3.       android:theme="@style/CodeFont">  

 

则application中的所有text字体都会改变,效果如下:

在每个<activity>标签中使用android:theme属性:

  1. <activity android:name=".MainActivity"  
  2.           android:label="@string/app_name"  
  3.           android:theme="@style/CodeFont">  

android:theme还可以配置android中已经存在的theme:

  1. <activity android:theme="@android:style/Theme.Translucent">  

如果想调整android已经定义好的theme,则可以通过自定义style来实现,例如:

  1. <color name="custom_theme_color">#b0b0ff</color>  
  2. <style name="CustomTheme" parent="android:Theme.Light">  
  3.     <item name="android:windowBackground">@color/custom_theme_color</item>  
  4.     <item name="android:colorBackground">@color/custom_theme_color</item>  
  5. </style>  

效果如下:

关于在<activity>中android的Theme的详细使用见:android Theme使用总结

 

根据android版本选择主题:

 

在android新的版本中增加了新的theme,如果想在新版中利用新theme同时又兼容旧版本,可以通过配置两个theme文件实现,例如在res/values目录下配置sytle.xml文件:

  1. <style name="LightThemeSelector" parent="android:Theme.Light">  
  2. </style>  

在res/values-11目录下配置文件style.xml:

  1. <style name="LightThemeSelector" parent="android:Theme.Holo.Light" mce_bogus="1">  
  2. </style>  

按照文档中说的在res下创建values-11目录,程序报错,需要找时间研究一下,说不定是android的一个bug。

 

使用Android提供的Style和Theme:

Android平台提供了大量的styles和themes,可以在android包中的R.style下找到,但是Android现在并未提供关于styles和themes的相关文档说明,具体可以参考styles.xml源码themes.xml源码 ,扫了一下,描述的很清楚。

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值