1.让应用继承DayNight主题
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
要和清单文件中的name保持一致
2.新建夜间模式资源文件夹:
在res目录下新建values-night
文件夹,然后在此目录下新建colors.xml
文件在夜间模式下的应用的资源。当然也可以根据需要新建drawable-night
,layout-night
等后缀为-night
的夜间资源文件夹。如下:
values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#009688</color>
<color name="colorPrimaryDark">#00796B</color>
<color name="colorAccent">#009688</color>
<color name="textColorPrimary">#616161</color>
<color name="viewBackground">@android:color/white</color>
<color name="colorDayNightChange">@android:color/holo_orange_dark</color>
</resources>
values/strings.xml
<resources>
<string name="app_name">DayNight</string>
<string name="day_night_label">日间模式</string>
</resources>
values-night/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#35464e</color>
<color name="colorPrimaryDark">#212a2f</color>
<color name="colorAccent">#212a2f</color>
<color name="textColorPrimary">#616161</color>
<color name="viewBackground">#212a2f</color>
<color name="colorDayNightChange">@android:color/holo_blue_dark</color>
</resources>
values-night/strings.xml
<resources>
<string name="app_name">DayNight</string>
<string name="day_night_label">夜间模式</string>
</resources>
3.acitivity_main.xml布局
<?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:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/day_night_label"
android:textSize="20sp"
android:textColor="@color/colorDayNightChange" />
<Button
android:id="@+id/day_night_change"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:text="日夜间模式切换"
android:textSize="20sp"
android:textColor="@color/colorDayNightChange"/>
</LinearLayout>
4.MainActivity.java
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
public class MainActivity extends AppCompatActivity {
private Button mDayNightChange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDayNightChange = (Button) findViewById(R.id.day_night_change);
mDayNightChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (mode == Configuration.UI_MODE_NIGHT_YES) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (mode == Configuration.UI_MODE_NIGHT_NO) {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
recreate();
}
});
}
}
可以在BaseActivity中写入固定的 ,然后其他Activity去继承
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
/**
* MODE_NIGHT_NO: 使用亮色(light)主题,不使用夜间模式
* MODE_NIGHT_YES:使用暗色(dark)主题,使用夜间模式
* MODE_NIGHT_AUTO:根据当前时间自动切换 亮色(light)/暗色(dark)主题
* MODE_NIGHT_FOLLOW_SYSTEM(默认选项):设置为跟随系统,通常为MODE_NIGHT_NO
*/
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
}
}