Android中自訂樣式(style)與主題(theme)

來源:互聯網
上載者:User

Android提供了許多可視的組件。通過自訂樣式和主題,可以避免用這些組件開發的應用看上去千篇一律。

樣式和主題都是通過預定義一系列屬性值來形成統一的顯示風格。區別是,樣式只能應用於某種類型的View;而主題剛好相反,它不能應用於特定的View,而只能作用於一個或多個Activity,或是整個應用。

以下結合具體例子說明如何定義樣式和主題:

1.定義樣式和主題
在工程中res/values/下添加styles.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- 定義my_style_1,沒有指定parent,用系統預設的 -->
  4. <style name="my_style_1">
  5. <!-- 定義與指定View相關的若干屬性 -->
  6. <item name="android:hint">load from style 1</item>
  7. </style>
  8. <!-- 定義my_style_2,用自訂的my_style_1作為parent -->
  9. <style name="my_style_2" parent="@style/my_style_1">
  10. <!-- 定義與指定View相關的若干屬性 -->
  11. <item name="android:textSize">30sp</item>
  12. <item name="android:textColor">#FFFF0000</item>
  13. <item name="android:hint">load from style 2</item>
  14. </style>
  15. <!-- 定義my_style_3,用android的EditText作為parent -->
  16. <style name="my_style_3" parent="@android:style/Widget.EditText">
  17. <!-- 定義與指定View相關的若干屬性 -->
  18. <item name="android:hint">"load from style 3"</item>
  19. <item name="android:textStyle">bold|italic</item>
  20. <item name="android:typeface">monospace</item>>
  21. <item name="android:background">@drawable/mybackground</item>
  22. </style>
  23. <!-- 定義MyTheme,用android的Theme作為parent -->
  24. <style name="MyTheme" parent="@android:style/Theme">
  25. <item name="android:textSize">20sp</item>
  26. <item name="android:textColor">#FF0000FF</item>
  27. <item name="android:hint">"load from style 3"</item>
  28. <item name="android:textStyle">bold|italic</item>
  29. <item name="android:typeface">monospace</item>>
  30. <item name="android:background">@drawable/gallery_selected_pressed</item>
  31. <item name="myStyle">@style/my_style_3</item>
  32. </style>
  33. </resources>

複製代碼

主題和樣式的定義方法類似,都是在<style>下添加N個<item>。
<style>下有兩個有用的屬性:
name - 以後引用時會用到;
parent - 可選,一些在自訂的style中沒有指定的屬性會繼承parent style中的值。parent可以是android預定義的resource,也可以是自己定義的style。
<item>下定義需要改變的屬性值。Android中能使用的屬性可以在<sdk>/docs/reference/android/R.styleable.html中查到;也可以用自己定義的屬性值;

2.使用主題
a)從AndroidManifest中指定,可以選擇Application應用級:

  1. <application android:theme="@style/MyTheme">

複製代碼

或是Activity級:

  1. <activity android:theme="@style/MyTheme">

複製代碼

b)從Java代碼中指定:

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setTheme(R.style.MyTheme);
  4. setContentView(R.layout.main);
  5. }

複製代碼

注意:setTheme必須在setContentView(),addContentView()或inflate()等執行個體化View的函數之前調用!

3.使用樣式
a)從layout的xml檔案中指定:

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

複製代碼

b)從Java代碼中指定:

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setTheme(R.style.MyTheme);
  4. setContentView(R.layout.main);
  5. LinearLayout ll = (LinearLayout)findViewById(R.id.llMain);
  6. EditText et = new EditText(this, null, R.attr.myStyle);
  7. ll.addView(et);
  8. }

複製代碼

-----------------
附件中是用到的具體例子
referenece:<android-sdk-windows-1.5_r2>/docs/guide/topics/ui/themes.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.