android应用程序如何设置样式

来源:互联网 发布:p2p网络借贷监管政策 编辑:程序博客网 时间:2024/06/06 07:00

android应用程序如何设置样式,包括样式定义、单个view设置样式 全局样式设置样式继承关系

 

1、样式定义

android的样式定义在res/values/style.xml文件中,类似web前端中将样式定义在某个css文件中,但android的style.xml是自动加载的,不需要手动import或link。目前还不了解android是否可以或怎么定义多个style文件。

如下是一组样式的定义

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">    <!-- 全局字体样式-->  
  2.     <style name="DefaultFontStyle">   
  3.         <item name="android:textSize">18px</item>  
  4.         <item name="android:textColor">#0000CC</item>  
  5.     </style>  
  6.       
  7.     <!-- 全局背景色-->  
  8.     <style name="DefaultBgColor" parent="@style/DefaultFontStyle">   
  9.         <item name="android:background">#F2F2F2</item>  
  10.     </style>  
  11.       
  12.     <!-- 全局样式-->  
  13.     <style name="DefaultStyle" parent="@style/DefaultBgColor">   
  14.     </style></SPAN>  
    <!-- 全局字体样式-->    <style name="DefaultFontStyle">         <item name="android:textSize">18px</item>        <item name="android:textColor">#0000CC</item>    </style>        <!-- 全局背景色-->    <style name="DefaultBgColor" parent="@style/DefaultFontStyle">         <item name="android:background">#F2F2F2</item>    </style>        <!-- 全局样式-->    <style name="DefaultStyle" parent="@style/DefaultBgColor">     </style>

a. android的样式定义是通过style标签完成的,通过添加item元素设置不同的属性值

b. 样式可以通过设置parent进行继承。上面的DefaultBgColor继承自DefaultFontStyle,而DefaultStyle又继承自DefaultBgColor,这样DefaultStyle就有了字体大小颜色、背景色的属性了。

c. android的主题样式和一般样式的定义是一样的,只是引用时不同,下面将会介绍

 

2、单个view如何设置样式

比如TextView,设置样式如下

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><TextView   
  2.     android:layout_width="match_parent"   
  3.     android:layout_height="wrap_content"  
  4.     android:text="我在做什么:"  
  5.     android:textSize="18px"  
  6.     android:textColor="#0000CC"  
  7.     /></SPAN>  
<TextView     android:layout_width="match_parent"     android:layout_height="wrap_content"    android:text="我在做什么:"    android:textSize="18px"    android:textColor="#0000CC"    />

也可以引用第一部分定义的样式,如下

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><TextView   
  2.     android:layout_width="match_parent"   
  3.     android:layout_height="wrap_content"  
  4.     android:text="我在做什么:"  
  5.     style="@style/DefaultStyle"  
  6.     /></SPAN>  
<TextView     android:layout_width="match_parent"     android:layout_height="wrap_content"    android:text="我在做什么:"    style="@style/DefaultStyle"    />

设置view的style属性进行样式调用,推荐使用此种方式将样式和布局分离。其他view及viewGroup设置相同。

 

对于单个view的更多属性可以参考http://developer.android.com/reference/android/R.styleable.html#View

或具体的某个view的sdk文档xml attribute.

 

3、全局样式设置

在web前端编程中,可以使用

[html] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">body {  
  2.     background: #cce8cf;  
  3.     color: #000;  
  4.     font-family: 宋体 verdana, tahoma;  
  5.     font-size: 18px;  
  6.     padding: 1px 2px 0 2px;  
  7.     counter-reset: section;  
  8. }</SPAN>  
body {background: #cce8cf;color: #000;font-family: 宋体 verdana, tahoma;font-size: 18px;padding: 1px 2px 0 2px;counter-reset: section;}

设置全局的样式

[html] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">div {  
  2.     margin-top: 10px;  
  3.     margin-bottom: 10px;  
  4. }</SPAN>  
div {    margin-top: 10px;margin-bottom: 10px;}

设置单个标签的样式

 

android中我们同样可以办到,只是这种全局样式被称作主题theme,比如对于整个应用默认字体都要18px,颜色为#0000CC,背景色为#F2F2F2,我们可以通过在AndroidManifest.xml设置application的android:theme属性完成,如下:

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><application android:theme="@style/DefaultStyle"></SPAN>  
<application android:theme="@style/DefaultStyle">

DefaultStyle即为第一部分中定义的主题,在第一部分中我们提到的主题和样式定义一样也是这个意思,只是引用的时候使用android:theme罢了。

 

下面为单个activity设置主题的代码

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><activity android:name=".AccountManageActivity"  
  2.       android:theme="@style/DefaultStyle"></SPAN>  
<activity android:name=".AccountManageActivity"      android:theme="@style/DefaultStyle">

activity的主题还有一些特殊设置,如

 

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">android:theme="@android:style/Theme.Dialog"</SPAN>  
android:theme="@android:style/Theme.Dialog"

为对话框样式设置 

主题的设置也可以在代码中通过setTheme(R.id.xx)完成。

 

接下来问题就出现了,如果一个应用设置了application的主题,设置了activity,设置了view的样式,那么view的各个样式属性值究竟是多少呢??

 

3、样式继承关系

android的样式采取和css中一样的覆盖、继承原则,和面向对象的子类覆盖父类属性、继承没有定义的父类属性值的原则是一样的。

 

如果一个TextView自己设置了样式,它的ViewGroup设置了样式,activity设置了主题,application设置了主题。

它会先读取自己样式的值,对于自己没有的样式向上查找第一个找到的值即为要采取的值。

 

依次读取的顺序为View自己的样式->上一层ViewGroup的属性值->上上层ViewGroup的属性值->…->activity主题->activity主题

 

例子如下

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)">    <!-- 全局字体样式-->  
  2.     <style name="DefaultFontStyle">   
  3.         <item name="android:textSize">18px</item>  
  4.         <item name="android:textColor">#0000CC</item>  
  5.     </style>  
  6.       
  7.     <!-- 全局背景色-->  
  8.     <style name="DefaultBgColor" parent="@style/DefaultFontStyle">   
  9.         <item name="android:background">#F2F2F2</item>  
  10.     </style>  
  11.       
  12.     <!-- 全局样式-->  
  13.     <style name="DefaultStyle" parent="@style/DefaultBgColor">   
  14.     </style>  
  15.   
  16.     <!-- textView字体样式-->  
  17.     <style name="TextViewFontStyle">   
  18.         <item name="android:textSize">20px</item>  
  19.     </style></SPAN>  
    <!-- 全局字体样式-->    <style name="DefaultFontStyle">         <item name="android:textSize">18px</item>        <item name="android:textColor">#0000CC</item>    </style>        <!-- 全局背景色-->    <style name="DefaultBgColor" parent="@style/DefaultFontStyle">         <item name="android:background">#F2F2F2</item>    </style>        <!-- 全局样式-->    <style name="DefaultStyle" parent="@style/DefaultBgColor">     </style>    <!-- textView字体样式-->    <style name="TextViewFontStyle">         <item name="android:textSize">20px</item>    </style>

application主题为

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><application android:theme="@style/DefaultStyle"></SPAN>  
<application android:theme="@style/DefaultStyle">

activity主题为

[xml] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><activity android:name=".AccountManageActivity"  
  2.       android:theme="@style/DefaultStyle"></SPAN>  
<activity android:name=".AccountManageActivity"      android:theme="@style/DefaultStyle">

textView样式设置如下

[java] view plaincopyprint?
  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><TextView   
  2.     android:layout_width="match_parent"   
  3.     android:layout_height="wrap_content"  
  4.     android:text="我在做什么:"  
  5.     style="@style/TextViewFontStyle"  
  6.     /></SPAN>  
<TextView     android:layout_width="match_parent"     android:layout_height="wrap_content"    android:text="我在做什么:"    style="@style/TextViewFontStyle"    />

 则textView中最终字体大小为20px,颜色采用activity中设置的0000CC