Android 中的Theme和Style使用,還是比較簡單的。
1、 首先在res/values/styles.xml的resource中定義三個樣式,分別為:
<style name="TextView"> <item name="android:textSize">38sp</item> <item name="android:textColor">#128</item> <item name="android:shadowRadius">1.0</item> <item name="android:background">#035</item> </style> <style name="EditText"> <item name="android:shadowColor">@android:color/black</item> <item name="android:shadowRadius">1.0</item> <item name="android:background">#312</item> <item name="android:foreground">#432</item> <item name="android:textAppearance">?android:attr/textAppearanceMedium</item> <item name="android:height">80dp</item> </style><style name="Button"> <item name="android:background">@android:drawable/edit_text</item> <item name="android:textAppearance">?android:attr/textAppearanceMedium</item></style>
然後在res/layout檔案下的activity_main.xml中的控制項中引用剛才定義的Style。
<TextView android:id="@+id/textView1" style="@style/TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" style="@style/Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/editText1" android:layout_marginTop="18dp" android:text="@string/hello_world" /> <EditText android:id="@+id/editText1" style="@style/EditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/textView1" android:layout_marginTop="19dp" android:ems="10" android:text="@string/hello_world" />
2、Android的Theme的使用
首先在res/values/themes.xml中定義Theme。
<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="Theme" parent="android:Theme.Light"> <item name="android:windowFullscreen">true</item> <item name="android:windowTitleSize">60dip</item> <item name="android:windowTitleStyle">@style/WindowTitle</item> <item name="android:background">#234</item> </style> <style name="WindowTitle"> <item name="android:singleLine">true</item> <item name="android:shadowColor">#658</item> <item name="android:shadowRadius">2.75</item> </style> </resources>
然後在AndroidManifest.xml中使用剛才定義的主題。
只要定義application的android:theme屬性為style/Theme即可。
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme" > <activity android:name="com.example.themedemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
也可以用setTheme(R.style.Theme)來調用主題。就不上傳了。