[Android记录]自定义主题

本文详细介绍了如何在Android应用中自定义主题和样式,包括如何为整个应用程序设置主题,以及如何为特定活动(activity)设置独立的主题。通过XML配置文件展示了具体的实现方法。
在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>

转载于:https://my.oschina.net/xesam/blog/122034

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值