【移動開發】Android中Theme和Style的使用

 當我們做項目到一定程度的時候,Android提供給我們的簡單的UI控件(雖然後期ADT插件更新中增加了不少)已經不能滿足我們的需要,一款成功的軟件不僅要功能強大,漂亮的界面同樣會吸引不少用戶!

   這裏,我將總結一下Android提供給我們的兩個重要的資源:Theme 和 Style。


1.Theme(是針對窗體級別的,可以改變窗體樣式)

官方文檔  

       Themes are Android's mechanism for applying a consistent style to an app or activity. The style specifies the visual properties of the elements that make up your user interface, such as color, height, padding and font size. To promote greater cohesion between all apps on the platform, Android provides three system themes that you can choose from when building apps for Ice Cream Sandwich:

      主題是Android的機制被應用一個相同風格的應用程序Activity中。樣式指定了視覺屬性的元素裝飾你的用戶界面,如顏色、高度、填充和字體大小。促進更大的凝聚力在所有平臺的應用程序,Android提供了三個系統的主題,您可以選擇在構建應用程序選擇不同的主題:

   Holo Light
   Holo Dark
   Holo Light with dark action bars

官方圖:

1.1.使用

   Android系統的themes.xml和style.xml(位於/base/core/res/res/values/)包含了很多系統定義好的style,建議在裏面挑個合適的,然後再繼承修改。以下屬性是在Themes中比較常見的,源自Android系統本身的themes.xml:

1
2
3
4
5
6
7
8
<style
   name="simple_dialog" parent="@android:style/Theme.Dialog">
       <item name="android:windowFrame">@null</item><!-- Dialog的WindowFrame框爲無 -->
       <item name="android:windowNoTitle">true</item>
       <item name="android:windowIsFloating">true</item><!-- 是否懸浮在activity上 -->
       <item name="android:windowIsTranslucent">true</item><!-- 是否半透明 -->
       <item name="android:backgroundDimEnabled">false</item><!-- 背景是否模糊 -->
   </style>

有人會問從哪裏學習這些主題?其實系統就自帶了很多,看下圖:

在AndroidManifest的Application中,點擊Theme選項,系統資源裏就有相當多的主題供你選擇

  A.應用到Application

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

   B.應用到Activity

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


2.Style(是針對窗體元素級別的,改變指定控件或者Layout的樣式)

 官方文檔:

  A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.

   風格是一個集合的屬性用於指定具有一定外觀和格式的視圖或窗口。一個風格可以指定屬性如高度、填充、字體顏色、字體大小、背景色等等。一個樣式定義在XML資源,獨立於XML指定佈局。

Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.

    風格在安卓中的份額就如同級聯樣式表在web設計中的設計,允許你將內容分割開來設計

   2.1簡單使用

   一個簡單地樣式

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

    可以在任何相同的地方被引用

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

2.2定義風格

   首先可以在res/values/下創建一個xml,用於定義style(注意:根結點必須是<resources>

1
2
3
4
5
6
7
8
9
<?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的名稱爲CodeFont,parent後面是父類的style,(當然,我們也可以不用繼承父類style),接下來每一個item定義一個屬性。定義屬性的最好方法就是在api文檔裏找到這個view的xml屬性,比如在EditText中有InputType這個屬性,那麼在你的style裏面你就可以來定義它。


   ok! 就是這些了!

      本文出自 “狂奔的蝸牛” 博客,出處http://smallwoniu.blog.51cto.com/3911954/1251507

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