colors.xml 中定义两组颜色,分别表示日间和夜间的主题色:
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<resources>
<color name=
"colorPrimary"
>#3F51B5</color>
<color name=
"colorPrimaryDark"
>#303F9F</color>
<color name=
"colorAccent"
>#FF4081</color>
<color name=
"nightColorPrimary"
>#3b3b3b</color>
<color name=
"nightColorPrimaryDark"
>#
383838
</color>
<color name=
"nightColorAccent"
>#a72b55</color>
</resources>
styles.xml 中定义两组主题,也就是日间主题和夜间主题:
<
resources
>
<!-- Base application theme. -->
<
style
name
=
"AppTheme"
parent
=
"Theme.AppCompat.Light.DarkActionBar"
>
<!-- Customize your theme here. -->
<
item
name
=
"colorPrimary"
>@color/colorPrimary</
item
>
<
item
name
=
"colorPrimaryDark"
>@color/colorPrimaryDark</
item
>
<
item
name
=
"colorAccent"
>@color/colorAccent</
item
>
<
item
name
=
"android:textColor"
>@android:color/black</
item
>
<
item
name
=
"mainBackground"
>@android:color/white</
item
>
</
style
>
<
style
name
=
"NightAppTheme"
parent
=
"Theme.AppCompat.Light.DarkActionBar"
>
<!-- Customize your theme here. -->
<
item
name
=
"colorPrimary"
>@color/nightColorPrimary</
item
>
<
item
name
=
"colorPrimaryDark"
>@color/nightColorPrimaryDark</
item
>
<
item
name
=
"colorAccent"
>@color/nightColorAccent</
item
>
<
item
name
=
"android:textColor"
>@android:color/white</
item
>
<
item
name
=
"mainBackground"
>@color/nightColorPrimaryDark</
item
>
</
style
>
</
resources
>
在主题中的 mainBackground
属性是我们自定义的属性,用来表示背景色:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:background
=
"?attr/mainBackground"
android:paddingBottom
=
"@dimen/activity_vertical_margin"
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
android:paddingRight
=
"@dimen/activity_horizontal_margin"
android:paddingTop
=
"@dimen/activity_vertical_margin"
tools:context
=
"com.yuqirong.themedemo.MainActivity"
>
<
Button
android:id
=
"@+id/btn_theme"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:text
=
"切换日/夜间模式"
/>
<
TextView
android:id
=
"@+id/tv"
android:layout_below
=
"@id/btn_theme"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:gravity
=
"center_horizontal"
android:text
=
"通过setTheme()的方法"
/>
</
RelativeLayout
>
public
class
MainActivity
extends
AppCompatActivity {
// 默认是日间模式
private
int
theme = R.style.AppTheme;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
// 判断是否有主题存储
if
(savedInstanceState !=
null
){
theme = savedInstanceState.getInt(
"theme"
);
setTheme(theme);
}
setContentView(R.layout.activity_main);
Button btn_theme = (Button) findViewById(R.id.btn_theme);
btn_theme.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme;
MainActivity.
this
.recreate();
}
});
}
@Override
protected
void
onSaveInstanceState(Bundle outState) {
super
.onSaveInstanceState(outState);
outState.putInt(
"theme"
, theme);
}
@Override
protected
void
onRestoreInstanceState(Bundle savedInstanceState) {
super
.onRestoreInstanceState(savedInstanceState);
theme = savedInstanceState.getInt(
"theme"
);
}
}
values/colors.xml :
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
resources
>
<
color
name
=
"colorPrimary"
>#3b3b3b</
color
>
<
color
name
=
"colorPrimaryDark"
>#383838</
color
>
<
color
name
=
"colorAccent"
>#a72b55</
color
>
<
color
name
=
"textColor"
>#FFFFFF</
color
>
<
color
name
=
"backgroundColor"
>#3b3b3b</
color
>
</
resources
>
<
resources
>
<!-- Base application theme. -->
<
style
name
=
"AppTheme"
parent
=
"Theme.AppCompat.Light.DarkActionBar"
>
<!-- Customize your theme here. -->
<
item
name
=
"colorPrimary"
>@color/colorPrimary</
item
>
<
item
name
=
"colorPrimaryDark"
>@color/colorPrimaryDark</
item
>
<
item
name
=
"colorAccent"
>@color/colorAccent</
item
>
<
item
name
=
"android:textColor"
>@color/textColor</
item
>
<
item
name
=
"mainBackground"
>@color/backgroundColor</
item
>
</
style
>
</
resources
>
public
class
MyApplication
extends
Application {
@Override
public
void
onCreate() {
super
.onCreate();
// 默认设置为日间模式
AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_NO);
}
}