Android Styles & Themes

什么是Style,什么是Theme?
      Style:是一个包含一种或者多种格式化属性的集合,我们可以将其用为一个单位用在布局XML单个元素当中。比如,我们可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。
      Theme:是一个包含一种或者多种格式化属性的集合,我们可以将其为一个单位用在应用中所有的Activity当中或者应用中的某个Activity当 中。比如,我们可以定义一个Theme,它为window frame和panel 的前景和背景定义了一组颜色,并为菜单定义可文字的大小和颜色属性,可以将这个Theme应用在你程序当中所有的Activity里。
 
定义Styles和Themes资源的XML文档的结构
      对每一个Styles和Themes,给<style>元素增加一个全局唯一的名字,也可以选择增加一个父类属性。在后边我们可以用这个名字来应用风格,而父类属性标识了当前风格是继承于哪个风格。在<style>元素内部,申明一个或者多个<item>,每一个<item>定义了一个名字属性,并且在元素内部定义了这个风格的值。
 
 
 
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <resources>   
  3.     <!-- 定义style -->   
  4.     <style name="myTextStyle" mce_bogus="1">        <item name="android:textSize">20px</item>   
  5.         <item name="android:textColor">#EC9237</item>   
  6.     </style>   
  7.     <style name="myTextStyle2" mce_bogus="1">       <item name="android:textSize">14px</item>   
  8.         <item name="android:textColor">#FF7F7C</item>   
  9.     </style>   
  10.  
  11.     <!-- 定义theme -->   
  12.     <style name="myTheme" mce_bogus="1">        <item name="android:windowNoTitle">true</item>   
  13.         <item name="android:textSize">14px</item>   
  14.         <item name="android:textColor">#FFFF7F7C</item>   
  15.     </style>   
  16.  
  17. </resources>   
 从上面的例子可以看出,定义上style与定义theme基本相同,只是使用的地方不同,还有就是在一些标签的使用上: style 作用于view,theme作用于application或者Activity。
 
一个使用的例子:
 
 
 
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:id="@+id/linear"   
  4.     android:orientation="vertical"   
  5.     android:layout_width="fill_parent"   
  6.     android:layout_height="fill_parent"   
  7.     >   
  8. <TextView   
  9.     style="@style/myTextStyle"     
  10.     android:layout_width="fill_parent"    
  11.     android:layout_height="wrap_content"    
  12.     android:gravity="center_vertical|center_horizontal"   
  13.     android:text="@string/hello"   
  14.     />   
  15.        
  16. <TextView   
  17.     style="@style/myTextStyle2"     
  18.     android:layout_width="fill_parent"    
  19.     android:layout_height="wrap_content"    
  20.     android:gravity="center_vertical|center_horizontal"   
  21.     android:text="www.google.cn"   
  22.     android:autoLink="all"   
  23.     />   
  24. </LinearLayout>  
本例中既使用了style也使用了theme,这就会造成冲突,在处理冲突时我个人实验后得出的结论是 style的优先级要高于theme。
 
需要注意的一个标签就是android:autoLink 定义它之后具有他的view会显示为蓝色带下划线的文本
,并且当单击这个超链接的时候会调用系统的浏览器,启动网页,同时android的不支持html的格式。
 
  
  
  1. @Override 
  2. public void onCreate(Bundle savedInstanceState) { 
  3.     super.onCreate(savedInstanceState); 
  4.     setTheme(R.style.myTheme); 
  5.     setContentView(R.layout.main);        
在代码中使用theme:  setTheme()函数就调用了自定义的theme
 
在AndroidManifest.xml中应用Theme:
        为了在当前所有的Activity当中使用Theme,可以打开AndroidManifest.xml 文件,编辑<application>标签,让其包含android:theme属性,值是一个主题的名字,例如:<application android:theme=”@style/NewTheme”>。
 
        如果只是想让程序当中的某个Activity拥有这个Theme,那么可以修改<activity>标签。Android中提供了几种内置的资源,有好几种Theme你可以切换而不用自己写。比如可以用对话框Theme来让你的Activity看起来像一个对话框。在manifest中定义,例如:<activity android:theme=”@android:style/Theme.Dialog”>
如果喜欢一个Theme,但是想做一些轻微的改变,只需要将这个Theme添加为parent。Android SDK为我们提供了很多现成的Theme,比如:我们修改Theme.Dialog Theme,继承Theme.Dialog来生成一个新的Theme。<style parent=”@android:style/Theme.Dialog”
 
继承了Theme.Dialog后,我们可以按照我们的要求来调整Theme。我们可以修改在Theme.Dialog中定义的每个item元素的值,然后我们 在Android Manifest 文件中使用NewDialogTheme而不是 Theme.Dialog。
 
style 与 theme在用法上的区别
 
 (note that in this case  style is not prefixed with the  android:
namespace; it is a custom local style and not one provided by the platform). 
 Styles can be taken one step further and used as themes. While a style refers to a 
set of attributes applied to a single View element, themes refer to a set of attributes 
being applied to an entire screen. Themes can be defined in exactly the same <style>
and <item> structure as styles are. To apply a theme you simply associate a style with 
an entire Activity, such as: android:theme="@android:style/[stylename]".
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值