android應用程序如何設置樣式

android應用程序如何設置樣式,包括樣式定義、單個view設置樣式 全局樣式設置樣式繼承關係

 

1、樣式定義

android的樣式定義在res/values/style.xml文件中,類似web前端中將樣式定義在某個css文件中,但android的style.xml是自動加載的,不需要手動import或link。目前還不瞭解android是否可以或怎麼定義多個style文件。

如下是一組樣式的定義

  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,設置樣式如下

  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"
    />

也可以引用第一部分定義的樣式,如下

  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前端編程中,可以使用

  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;
}

設置全局的樣式

  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屬性完成,如下:

  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><application android:theme="@style/DefaultStyle"></SPAN>  
<application android:theme="@style/DefaultStyle">

DefaultStyle即爲第一部分中定義的主題,在第一部分中我們提到的主題和樣式定義一樣也是這個意思,只是引用的時候使用android:theme罷了。

 

下面爲單個activity設置主題的代碼

  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的主題還有一些特殊設置,如

 

  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主題

 

例子如下

  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主題爲

  1. <SPAN style="BACKGROUND-COLOR: rgb(255,255,255)"><application android:theme="@style/DefaultStyle"></SPAN>  
<application android:theme="@style/DefaultStyle">

activity主題爲

  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樣式設置如下

  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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章