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" >
Github : Demo