Android通过改变主题实现夜间模式

http://www.cnblogs.com/yuanzhanxue/p/3470820.html

这篇文章里提供了三种方法,非常感谢博主,帮助很大啊。

考虑到工作量的问题,第一种方法显然不太合适。如果采用加遮光罩的方法,好处是简便易行,缺点也是很明显的。因为一般日间模式都是白底黑字,如果加了遮光罩,黑字就会变得不太明显,也就是用户需要的信息反而变得更不明显了,这显然不是我们想要的。所以最终采用了改theme的这个方法。基本照着那篇博文的方法来实现的,但是因为我们这里是要改整个APP的theme,所以传入Context的时候注意传入Application 的Context就可以了。

具体方法如下:

  • 1 . 在工程 res/values/attrs.xml 文件中,增加自定义属性。
  • 2 . 在工程增加自定义属性用到的颜色和引用。
  • 3 . 在工程 res/values/styles.xml 文件中,增加“AppSunTheme” 和“AppNightTheme”,parent 均为AppBaseTheme. 同时在这两个styles中一一对应的加入attrs.xml文件中的属性。
  • 4 . 将layout中.xml中需要根据主题改变的元素的background 和 color 设为自定义styles中的属性。
  • 5 . 在工程中加入NightModeUtils类来配置应用主题。因为这里要改变整个APP的主题,所以传给NightModeUtils的Context应该是Application的Context。
  • 6 . 在每个Activity中增加调用nightModeUtils类的设置主题方法。注意要加在setContentView方法之前。
  • 7 . 将Mainfest配置文件中Application 的theme设为默认的AppSunTheme.
  • 8 . 最后需要注意一点,settheme方法只能在onCreate方法中实现,所以如果要改变当前Activity的注意要将当前Activity先finish再重新启动Activity。
代码参考:

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NightMode">
        <attr name="day_night_background" format="color" />
        <attr name="day_night_background2" format="color" />
        <attr name="day_night_text_color" format="color" />
    </declare-styleable>
</resources>

这里需要注意一下的是,如果需要改变的只是背景色等这些用color 就可以完成的,就把format设成color。如果是要用到drawable 这些元素,就要设成reference。

具体可以参考这里 : http://blog.csdn.net/mayingcai1987/article/details/6216655
styles.xml

 <!-- Application theme. -->
    <style name="AppSunTheme" parent="AppBaseTheme">
        <item name="day_night_background">@color/light_color</item>
        <item name="day_night_background2">@color/light_color2</item>
        <item name="day_night_text_color">@color/night_color</item>
    </style>

    <style name="AppNightTheme" parent="AppBaseTheme">
        <item name="day_night_background">@color/night_color</item>
        <item name="day_night_background2">@color/night_color</item>
        <item name="day_night_text_color">@color/light_color</item>
    </style>

layout中 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="?attr/day_night_background">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:text="这是第二页"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:textColor="?attr/day_night_text_color"
         android:background="?attr/day_night_background2"/>

</LinearLayout>
MainActivity 中调用 NightModeUtils

NightModeUtils.onActivityCreateSetTheme(this); //在onCreat 方法中调用
设定DayNightMode, 注意要传入Application的Context 就可以了
button3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int theme = NightModeUtils.getDayNightMode(MainActivity.this);
				Context context = getApplicationContext();
				if (theme == NightModeUtils.THEME_SUN)
					NightModeUtils.setDayNightMode(context,
							NightModeUtils.THEME_NIGHT);
				else
					NightModeUtils.setDayNightMode(context,
							NightModeUtils.THEME_SUN);
				// 注意改过主题后一定要,把activity finish在重开一遍,因为更改主题只能在oncreat中进行
				MainActivity.this.finish();
				MainActivity.this.startActivity(new Intent(MainActivity.this, MainActivity.this.getClass()));
			}
		});
Mainfest.xml

把theme 改为我们写的Theme

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppSunTheme" >


如果设定多个theme就可以实现“换皮肤”的功能了。Demo地址如下,直接在实现验证码的工程上改的,大家看看就好,欢迎指正,交流:-D


Github :  Demo









评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值