现在的位置: 首页 > 综合 > 正文

自定义Android主题

2013年07月30日 ⁄ 综合 ⁄ 共 1837字 ⁄ 字号 评论关闭

在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>

 

抱歉!评论已关闭.