自訂Android主題

來源:互聯網
上載者:User

在AndroidManifest.xml中,可以定義整個app的主題,也可以定義單個activity的主題。下面的樣本分別給application和activity定義了一個主題: 

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"    <application android:theme="custom_application_theme"      <activity android:name=".Activity"android:theme="custom_activity_theme"/>    </application></manifest>


接下來的問題就是定義自己的主題了,事實上,主題(theme)只不過是一個作用範圍更大的樣式(style)集,所以定義theme和定義style的格式是一樣的。如果使用eclipse建立一個4.0以上的空白工程,可以看到如下的AndroidManifest.xml: 

    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.yg.xesam.net.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>

查看@style/AppTheme的內容可以看到最終的樣式定義(系統的themes檔案放在sdk對應平台的/res/values/themes.xml中)。

自訂theme可以定義在styles.xml中,但是為了方便,也可以單獨定義在自己建立的themes.xml中: 

<resources xmlns:android="http://schemas.android.com/apk/res/android">    <style name="custom_application_theme" parent="android:Theme.Light"></style>    <style name="custom_activity_theme" parent="custom_application_theme"></style></resources>


具體內容可以參考sdk內建的themes.xml檔案,比如在繼承parent(android:Theme.Light)樣式的基礎上再定義程式全螢幕,是否有標題列等等。 
下面的例子中: 
custom_application_theme定義沒有標題列並且全屏,custom_activity_theme覆蓋了custom_application_theme的android:windowNoTitle屬性,所以custom_activity_theme是全屏但是保留標題列 

<resources xmlns:android="http://schemas.android.com/apk/res/android">    <style name="custom_application_theme" parent="android:Theme.Light">        <item name="android:windowNoTitle">true</item>        <item name="android:windowFullscreen">true</item>    </style>    <style name="custom_activity_theme" parent="custom_application_theme">        <item name="android:windowNoTitle">false</item>    </style></resources>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.