android自訂style主題樣式 )

來源:互聯網
上載者:User

像HTML/CSS中的style一樣,android也可以使用自訂的style樣式

一般是在value 檔案夾下面建一個styles.xml檔案

樣式是用於描述一個View或是一個視窗的顯示內容的集合,樣式可以指定如高度,填充,字型顏色,字型大小,背景顏色等屬性。樣式是從布局檔案中分離出來的一個XML資源檔。Android中的樣式就像Web開發中的css樣式表,它使用我們的樣式獨立於內容進行設計開發。
例如,通過使用一個樣式可以讓如下的布局檔案

Xml代碼  
  1. <TextView  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:textColor="#00FF00"  
  5.     android:typeface="monospace"  
  6.     android:text="@string/hello" />  
<TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:textColor="#00FF00"    android:typeface="monospace"    android:text="@string/hello" />

 

簡化為

Xml代碼  
  1. <TextView  
  2.     style="@style/CodeFont"  
  3.     android:text="@string/hello" />  
<TextView    style="@style/CodeFont"    android:text="@string/hello" />

 

所有和樣式有關的屬性都被從布局XML檔案中移動到一個叫“CodeFont”的樣式定義中,然後使用一個style屬性指定樣式名稱。你將會在以下的內容中看到如何定義一個樣式。

應用於一個Activity或應用程式的樣式稱為主題(theme),而不是剛才說的一個View。所有在當前Activity或應用下的視圖(VIEW)都會應用相同的主題樣式。例如,您可以讓一個Activity使用”CodeFont”主題,那麼這個Activity下的所有視圖的的文本都將是綠色等寬字型。

 

定義樣式

定義樣式我們需要在 res/values/目錄下建立一個XML檔案,檔案名稱自已隨便命名,但必須以.xml為檔案尾碼。Xml的根節點必須為。
我們用style標籤來定義一個樣式,用標籤來定義樣式屬性。如下所示:

Xml代碼  
  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>  
<?xml version="1.0" encoding="utf-8"?><resources>    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">        <item name="android:layout_width">fill_parent</item>        <item name="android:layout_height">wrap_content</item>        <item name="android:textColor">#00FF00</item>        <item name="android:typeface">monospace</item>    </style></resources>

 

Style標籤的name屬性是必須有的,節點可以定義顏色、高度或者是另一個資源的引用。所有節點的子節點在編譯時間都會做為應用程式的一個資源。所以我們可以通過style節點的name屬性值來引用這個資源。比如在布局檔案中使用 @style/CodeFont來引用這個樣式。parent 屬性是可選的,用它來標識本樣式是繼承哪個樣式,在父樣式中的所有屬性都將被應用於本樣式中,同時可以覆蓋父樣式中的樣式(和java的繼承相似)。

 

樣式的繼承
這裡有兩種方式可以實現樣式繼承,如上例中所示的,我們可以在定義樣式時使用parent屬性來繼承樣式,使用這種方式,我們可以繼承一個我們自己定義好的樣式,也可以繼承一個android平台內建的樣式(後文中會介紹android平台內建的所有樣式)。如下所示,我們繼承一個android平台預設的文本樣式,並把它的字型顏色改為我們需要的顏色。

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

 另一種繼承的方式是使用使用者自訂的樣式作為首碼即可。這種方式只適用於繼承使用者自訂樣式。如下所示:

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

 這樣,新定義的樣式就會繼承CodeFont樣式的所有屬性,然後把字型顏色變為#FF0000。我們可以這樣引用新的樣式: @style/CodeFont.Red
相同的方法,我們可以再繼承下去,如下所示:

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

 

這樣新的樣式文子的大小就和CodeFont.Red樣式不同了。

樣式屬性

到這裡為止,你已經知道了如何定義一個樣式,你還需要知道有多少種樣式屬性可以通過節點來定義。你可能已經對其中的一些比較熟悉了,如layout_width 、textColor等。當然,還有很多的樣式屬性是你可以使用的。

最好的方法查詢一個視圖支援哪些樣式屬性的方法是查詢檢視類的文檔中XML Attributes表格,如TextView的XML attributes如連結中所示: http://www.ideasandroid.com/android/sdk/docs/reference/android/widget/TextView.html#lattrs

如需所有可用的樣式屬性的,請參閱R.attr: http://www.ideasandroid.com/android/sdk/docs/reference/android/R.attr.html
不是所有的視圖都支援上面的樣式屬性,如果遇到不支援的樣式屬性,您定義的屬性將會被忽略。

應用樣式和主題

應用樣式和主題的方法很簡單,在布局定義檔案(layout)中,使用style屬性直接引用樣式資源,如下所示:

Xml代碼  
  1. <TextView  
  2.     style="@style/CodeFont"  
  3.     android:text="@string/hello" />  
<TextView    style="@style/CodeFont"    android:text="@string/hello" />

 在AndroidManifest.xml檔案中可以引用樣式主題,可以為一個Activity定義一個主題,也可以為整個應用程式定義一個主題,如下所示:

Xml代碼  
  1. <application android:theme="@style/CustomTheme">  
  2. 或者   
  3. <activity android:theme="@style/CustomDialogTheme">  
<application android:theme="@style/CustomTheme">或者<activity android:theme="@style/CustomDialogTheme">

 

Android平台的樣式和主題

樣式見:http://www.ideasandroid.com/android/sdk/styles.xml
主題見:http://www.ideasandroid.com/android/sdk/themes.xml

 

轉載請註明出處 http://www.ideasandroid.com/archives/322

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.