歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android風格設計(style)

Android風格設計(style)

日期:2017/3/1 10:21:45   编辑:Linux編程

Android的風格設計(style)是一個很重要的功能,因為它可以讓應用程序裡的控件(widget)個性化。風格設計的使用如下:

  • 在Android的項目裡以XML的資源來定義風格
  • 一個Android項目可以定義多個風格
  • 讓widget套用其中的一個樣式

Android的style功能,主要的對象是widget,風格是為了套用到widget上;另外Android提供布景(theme)功能,可以做更大范圍的套用。

相關閱讀:

Android的布景設計(theme) http://www.linuxidc.com/Linux/2012-05/61361.htm
Android事件監聽器(Event Listener) http://www.linuxidc.com/Linux/2012-05/61186.htm

下面是一個風格定義的具體例子:

在/res/values/目錄下建立一個新文件style.xml,編輯內容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="myText">
  4. <item name="android:textSize">18sp</item>
  5. <item name="android:textColor">#00FF00</item>
  6. </style>
  7. <style name="myButton">
  8. <item name="android:background">#00BFFF</item>
  9. </style>
  10. </resources>

style.xml語法說明:

  1. 在<resource>標簽定義資源項目,<style>標簽用來定義風格資源;
  2. <style>的name屬性定義風格名稱,widget使用此名稱套用;
  3. <item>標簽定義此風格的內容;
  4. textSize —— 字體大小
  5. textColor —— 字體顏色
  6. background —— 背景
  7. 更多,參考Android Reference

定義好style後,就可以讓widget套用。

讓widget套用定義好的style方法很簡單,只需在main.XML中的widget項目屬性添加定義好的style name就可以了,編輯main.XML:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. style="@style/myText"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello" />
  11. <Button
  12. android:id="@+id/btn"
  13. style="@style/myButton"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content" />
  16. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved