日夜间模式切换

来源:互联网 发布:视频网站 python 编辑:程序博客网 时间:2024/05/01 18:11

  日夜间模式切换两种方式

第一种

创建values-night与drawable-night文件夹,将values中的colors.xml与strings.xml文件copy到values-night中,

切换日夜间模式中,系统自己会寻找文件,切换的夜间时,就会自动使用drawable-night,values-night中的设置


原values中color.xml的内容

原values中strings.xml的内容


values-night中color.xml的内容


values-night中strings.xml的内容


原values中styles.xml的内容,其中继承类发生变化,继承的是:Theme.AppCompat.DayNight.DarkActionBarT:

drawable与drawable-night中存放图片,如果你想要切换日夜间模式,图片也相应变化,就必须将相对应的图片名字起成相同的名字。


一切设置好之后需在布局xml文件中引用,假如你想在切换日夜间模式时,文字字体颜色发生变化,就将textColor引用你已经定义好的colors.xml的main_textView


定义一个MyApplication类继承Application类

public class MyApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        if (getSharedPreferences("theme", MODE_PRIVATE).getBoolean("night_theme", false)) {            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);        }    }}
MainActivity类

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button btnChangeTheme = (Button) findViewById(R.id.btnChangeTheme);        btnChangeTheme.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //切换日夜间模式                int uiMode;                uiMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;                switch (uiMode) {                    case Configuration.UI_MODE_NIGHT_YES:                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);                        getSharedPreferences("theme", MODE_PRIVATE).edit().putBoolean("night_theme", false).commit();                        break;                    case Configuration.UI_MODE_NIGHT_NO:                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);                        getSharedPreferences("theme", MODE_PRIVATE).edit().putBoolean("night_theme", true).commit();                        break;                }                //重建                recreate();            }        });    }

第二种

在values包下创一个attrs.xml,其中内容为:

<?xml version="1.0" encoding="utf-8"?><resources  <attr name="textColorValue" format="color"></attr>    <attr name="textContent" format="string"></attr></resources>
在values下的strings.xml中新加两个字段:夜间模式与日间模式

<resources>    <string name="app_name">ChangeThemeTwo</string>    <string name="change_to_night">夜间模式</string>    <string name="change_to_day">日间模式</string></resources>
在values下的styles.xml中的更改

<resources>    <!-- Base application theme. 日间模式-->    <style name="day_theme" 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:windowBackground">@color/white</item>        <!--日间模式对应的字体颜色 和日间模式对应的文本内容-->        <item name="textColorValue">@color/black</item>        <item name="textContent">@string/change_to_night</item>    </style>        <!-- Base application theme. 夜间模式-->    <style name="night_theme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/black</item>        <item name="colorPrimaryDark">@color/dark_bg</item>        <item name="colorAccent">@color/dark_bg1</item>        <item name="android:windowBackground">@color/black</item>        <!--夜间模式对应的字体颜色 和夜间模式对应的文本内容-->        <item name="textColorValue">@color/white</item>        <item name="textContent">@string/change_to_day</item>    </style></resources>
在AndroidManifest.xml清单文件中有所更改,theme中需更改为使用day_theme日间模式

在自己布局文件xml中,引用方式如下:


创建一个ThemeUtil类

public class ThemeUtil {    //我当前应用的主题    private static int theme = 0;    //日间模式主题    private static final int DAY_THEME = 0;    //夜间模式主题    private static final int NIGHT_THEME = 1;    public static void onActivityCreateSetTheme(Activity activity){        switch (theme){            case DAY_THEME:                activity.setTheme(R.style.day_theme);                break;            case NIGHT_THEME:                activity.setTheme(R.style.night_theme);                break;        }    }    //点击按钮改变对应得主题    public static void ChangeCurrentTheme(Activity activity) {        //1、改变当前主题的theme变量        switch (theme) {            case DAY_THEME:                theme = NIGHT_THEME;                break;            case NIGHT_THEME:                theme = DAY_THEME;                break;        }        //2、重启这个activity        activity.finish();        //为了切换没换,设置了动画效果       activity.overridePendingTransition(R.anim.sliding_in, R.anim.sliding_out);        activity.startActivity(new Intent(activity, activity.getClass()));    }}

在res下创建anim存放动画布局

sliding_in中的内容

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:fromAlpha="0.0"    android:toAlpha="1.0"    android:duration="1000"></alpha>
sliding_out中的内容

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:fromAlpha="1.0"    android:toAlpha="0.0"    android:duration="1000"></alpha>

MainActivity类

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //设置对应的主题 ,在ui创建好之后设置主题无效,所以要放到setContentView()方法前面setTheme()        ThemeUtil.onActivityCreateSetTheme(MainActivity.this);        setContentView(R.layout.activity_main);        Button btnChangeTheme = (Button) findViewById(R.id.btnChangeTheme);        btnChangeTheme.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //切换日夜间模式                ThemeUtil.ChangeCurrentTheme(MainActivity.this);            }        });    }}

原创粉丝点击