2013-02-08 68 views

回答

4

創建一個名爲styles.xml在應用程序的資源文件/目錄值。添加一個根節點。 對於每種樣式或主題,添加一個具有唯一名稱的元素,以及可選的父屬性。該名稱用於稍後引用這些樣式,並且父級指示要繼承的樣式資源。 在元素內部,聲明一個或多個元素的格式值。每個用name屬性標識其樣式屬性,並在元素內定義其樣式值。 然後,您可以從其他XML資源,清單或應用程序代碼中引用自定義資源。

主題是應用於整個活動或應用程序中的樣式,

<style name="MyTheme" parent="android:Theme.Light"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowBackground">@color/translucent_red</item> 
    <item name="android:listViewStyle">@style/MyListView</item> 
</style> 

<style name="MyListView" parent="@android:style/Widget.ListView"> 
    <item name="android:listSelector">@drawable/ic_menu_home</item> 
</style> 

要定義風格,保存在XML文件中的/ RES /值您項目的目錄。 XML文件的根節點必須是。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="text"> 
     <item name="android:padding">4dip</item> 
     <item name="android:textAppearance">?android:attr/textAppearanceLarge</item> 
     <item name="android:textColor">#000000</item> 
    </style> 
    <style name="layout"> 
     <item name="android:background">#C0C0C0</item> 
    </style> 
</resources> 

在AndroidManifest.xml中應用主題的活動要風格:

<activity 
     android:name="com.myapp.MyActivity" 
     ... 
     android:theme="@style/MyTheme" 
     />