facebook廣告





123

2017年1月28日 星期六

佈景(Theme)初體驗

http://www.jollen.org/blog/2009/06/jollen-android-programming-18.html
佈景(theme)是可以大範圍套用的UI美化功能,其套用範圍為「整個螢幕」,從程式碼的角度來看,佈景可以套用到以下二個範圍:
  • 整個應用程式(application)
  • 整個activity
接下來,我們以一個很簡單的例子,來說明如何套用佈景到application。在一些應用,我們可能不想要顯示視窗標題(title),怎麼做出這個功能呢?利用佈景設定的方式即可達成。以下是實作方法。
在styles.xml裡加入以下內容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="myTheme">
     <item name="android:windowNoTitle">true        
    </style> 
</resources>
修改AndroidManifest.xml,在標籤裡加上「theme」屬性:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.moko.hellotheme"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name"
     android:theme="@style/myTheme">
        <activity android:name=".HelloTheme"
                  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>
</manifest>
執行結果:
theme-1.png
圖1: HelloTheme的執行結果
在這個範例裡,我們並沒有修改任何的程式碼,其原理是透過佈景設定的方法。定義佈景的方式與定義樣式(styles)相同,同樣是在styles.xml裡以<item>標籤來定義。
以下是使用HelloTheme的說明:
1. <item>的name屬性為android:windowNoTitle時,表示定義是否要顯示視窗標題,在此設定為true,表示不要有視窗標題
2. 在<application>標籤裡加上theme屬性,將佈景套用到應用程式
佈景除了能套用到應用程式外,也能套用到activity。如何套用佈景到activity呢?只要在<activity>裡加入theme屬性即可,做法與<application>相同。

沒有留言:

張貼留言