說明如何套用佈景到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>相同。

 

在使用新版Eclipse開發Android程式時

創建Android Project後會多了res/values/styles.xml的樣式檔案

事實上這是讓Android讀取的預設的樣式定義檔

例如要讓整個程式的基本樣式為亮色系或暗色系~

或者要隱藏標題全螢幕等等, 皆可以在這個檔案上進行修改...

 

而相關語法參數如下: 

android:theme="Theme.Light"  背景為白色

android:theme="Theme.Light.NoTitleBar"  白色背景並無標題欄 

android:theme="Theme.Light.NoTitleBar.Fullscreen"白色背景,無標題欄,全螢幕

android:theme="Theme.Black"  背景黑色

android:theme="Theme.Black.NoTitleBar"  黑色背景並無標題欄

android:theme="Theme.Black.NoTitleBar.Fullscreen"黑色背景,無標題欄,全螢幕

android:theme="Theme.Wallpaper"  用系統桌面為應用程式背景

android:theme="Theme.Wallpaper.NoTitleBar"  用系統桌面為應用程式背景,且無標題欄

android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen"  用系統桌面為應用程式背景,無標題欄,全螢幕

android:theme="Translucent"

android:theme="Theme.Translucent.NoTitleBar"

android:theme="Theme.Translucent.NoTitleBar.Fullscreen"

android:theme="Theme.Panel"

android:theme="Theme.Light.Panel"

android:theme="@android:style/Theme.Dialog"  將一個Activity顯示為能話框模式

android:theme="@android:style/Theme.NoTitleBar"不顯示應用程式標題欄

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"不顯示應用程式標題欄,並全螢幕

arrow
arrow
    全站熱搜

    忽倫 發表在 痞客邦 留言(1) 人氣()