Android主题theme和风格style总结

用到了Android的主题和风格,感觉很多地方需要总结和记录下来。其实主题和风格是有很大的作用的,特别是界面要求比较高的客户端。

Style:是一个包含一种或者多种格式化属性的集合,我们可以将其用为一个单位用在布局XML单个元素当中。比如,我们可以定义一种风格来定义文本的字号大小和颜色,然后将其用在View元素的一个特定的实例。 

如何定义style
style也属于resource,所以要在resource下定义,就像定义string,color一样
定义style,需要指定name,style通常包含一个或多个item,每个item的name是android view的属性的名字,值则是对应相关属性的值

style的继承

可以给style指定parent,从而可以继承和覆盖parent style的属性,parent取值是另外一个style,如果是继承自自己定义的style,只需要在命名style时增加前缀,这个前缀就是即将继承的style的名字

例如CodeFont是一个自己定义的style,那么下面的style,CodeFont.Red,则继承了CodeFont,只是文本的颜色修改成了红色

  1. <style name="CodeFont.Red"> 红色  
  2.         <item name="android:textColor">#FF0000</item>   
  3.  </style>  
  4.  <style name="CodeFont.Red.Big">  红色,并且大字体  
  5.         <item name="android:textSize">30sp</item>   
  6.  </style>  

也可以继承平台的style,可继承的样式请参照绍docs/guide/topics/ui/themes.html#PlatformStyles

  1. <style name="CodeFont" parent="@android:style/TextAppearance">   

如果父样式的值不符合你的需求,你也可以对它进行修改,和CSS中的覆盖效果一样,都是以最后的为准,  

在style中可以定义的属性
都有哪些属性在style的定义里是有效的呢?具体请参考docs/reference/android/R.attr.html
在view上使用style时,对view有效的属性起作用,无效的则会忽略
有一些属性对view无效,只对theme有效,在R.attr定义中以window开头的一些属性只对theme有效

style的使用
如果给view指定style,那么这个style只对该view有效
如果给viewgroup指定style,那么viewgroup下的元素也不会应用这个style,除非特别指定
给view指定style时,没有android:前缀,而只是style

下面是具体用法:

首先在res/values下新建一style.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="TitleStyle">  
  4.         <item name="android:textSize">18sp</item>  
  5.         <item name="android:textColor">#ec9237</item>  
  6.     </style>  
  7.     <style name="Title" parent="@style/TitleStyle">  
  8.         <item name="android:textSize">5sp</item>  
  9.     </style>  
  10. </resources>  

在layout.xml中的应用:

  1. <EditText android:layout_height="wrap_content"   
  2.     android:text="EditText"   
  3.     style="@style/Title"  
  4.     android:layout_width="fill_parent"   
  5.     android:id="@+id/editText1"></EditText>  

其实style就像是一组属性的组合, 可以看做当在view中引用style时,是顺序执行style中的item里面的每个属性,对view进行设定而已。因为可能有多个view都是需要设置相同的属性,。所以把这些view的属性单独写出,提高重用性。 

theme:就像风格一样,主题依然在<style>元素里边申明,也是以同样的方式引用。不同的是你通过在Android
Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个Activity,但是主题是
不能应用在某一个单独的View里,所以配置文件的属性也就是窗口等的主题样式。
 

定义一个主题:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="theme1">  
  4.         <item name="android:windowNoTitle">true</item>  
  5.         <item name="android:windowFullscreen">?android:windowNoTitle</item>  
  6.     </style>  
  7. </resources>  

下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:

  1. <application android:icon="@drawable/icon" android:label="@string/app_name"    
  2.     android:theme="@style/theme1">    
  3.     <activity android:name=".MessageShowActivity" android:label="@string/app_name"    
  4.         android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"    
  5.         android:theme="@style/theme2">    
  6.     </activity>    
  7. </application>  

除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:

setTheme(R.style.theme1);

注意:我们用了@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目
中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过。

style和theme的区别:

尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。样式用在单独的View,如:EditText、TextView等;主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。

另外android系统也定义了一些主题,例如:

<activity android:theme="@android:style/Theme.Dialog">,该主题可以让Activity看起来像一个对话框,

<activity android:theme="@android:style/Theme.Black.NoTitleBar">Variant of the light theme with no title bar,系统自带的黑色主题。如果需要查阅这些主题,可以在文档的reference-->android-->R.style 中查看。

 

 

 

系统自带的Theme:
android以及为我们定义好了一些theme,需要是我们直接可以拿来使用。
常用的Theme通常如下:
 android:theme="@android:style/Theme.Dialog"将一个activity显示为对话框模式
 android:theme="@android:style/Theme.NoTitleBar"不显示应用程序标题栏
 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"不显示应用程序标题栏,并全屏
 android:theme="@android:style/Theme.light"背景为白色
 android:theme="@android:style/Theme.light.NoTitleBar" 白色背景,无标题栏
 android:theme="@android:style/Theme.light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏
 android:theme="@android:style/Theme.Black"背景为黑色
 android:theme="@android:style/Theme.Black.NoTitleBar" 黑色背景,无标题栏
 android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏

  android:theme="@android:style/Theme.Wallpaper"用系统桌面为应用程序背景
  android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景
,无标题栏
  android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景
,无标题栏,全屏

定义自己的Theme:
Theme的写法和style很相似,也为:
<style name="MyTheme"[parent="PARENT"]>
<item name="[ATTR]">[VALUE]</>
</style>

Theme的属性在Android的文档中并没有介绍,不过我们可以从系统自带的theme中对其进行了解:
一下是我们从android系统本身所带的theme.xml中提取出来的一些常用的属性:
<item name="windowBackground">@android:drawable/screen_background_dark</item>

<item name="windowFrame">@null</item>

<item name="windowNoTitle">false</item>
<item name="windowFullscreen">false</item>
<item name="windowFloating">false</item>

<item name="windowBackground">@android:drawable/screen_background_dark</item>


 

猜你喜欢

转载自wenzongliang.iteye.com/blog/1894132