android 画面风格切换(夜间模式/日间模式) (有小坑没解决)

谨以此文纪念踩过的小坑坑.



结构目录



首先 , color.xml,  定义每一套需要用到的color

例如:   我这里是用两套来举例说明

<?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="colorPrimaryq">#565760</color>
    <color name="colorPrimaryDarkq">#ffffff</color>
    <color name="colorAccentq">#16c822</color>
</resources>

然后, 定义style 里面的theme

需要注意的是需要继承一下,然后调用的是继承后的

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="textLight">@color/colorPrimary</item>
        <item name="textNight">@color/colorPrimaryDark</item>
        <item name="appBg">@color/colorAccent</item>
    </style>

    <style name="AppThemeNight" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="textLight">@color/colorPrimaryq</item>
        <item name="textNight">@color/colorPrimaryDarkq</item>
        <item name="appBg">@color/colorAccentq</item>
    </style>

    // 需要注意的是这里需要继承一下, 以后调用的是这个
    <style name="AppThemeII" parent="AppTheme"/>
    <style name="AppThemeNightII" parent="AppThemeNight"/>


</resources>


继续attrs 文件,

后面的format 是转化格式, 自行百度了解

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="textLight" format="reference|color" />
    <attr name="textNight" format="reference|color" />
    <attr name="appBg" format="reference|color" />
</resources>



以上values 文件中就定义完成



那么 在MainActivity 里面调用代码:

需要注意的是, 切换theme 需要写在 setContentView( )   前面;

public class MainActivity extends AppCompatActivity {

    // true 为白天 false 为黑天

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sharedPreferences = this.getSharedPreferences("CNBLOG", Context.MODE_PRIVATE);
        boolean a =  sharedPreferences.getBoolean("theme", false);

        if (a) {
            
           setTheme(R.style.AppThemeII);
        } else {
            setTheme(R.style.AppThemeNightII);
        }

        setContentView(R.layout.activity_main);

        TextView tv_theme = (TextView) findViewById(R.id.theme);
        tv_theme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean a1 = SPHelper.getTheme(v.getContext());
//                Log.d("MainActivity", "a:" + a1);
//                Log.d("MainActivity", "SPHelper.getTheme(v.getContext()):" + SPHelper.getTheme(v.getContext()));
                SharedPreferences sharedPreferences = getSharedPreferences("CNBLOG", Context.MODE_PRIVATE);
                sharedPreferences.edit().putBoolean("theme", !a1).commit();
                final Intent themeintent = getIntent();
                // 重新入栈 刷新activity
               themeintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(themeintent);
                overridePendingTransition(0, 0);

            }
        });
    }
}


R.layout.activity_main:

颜色由

?attr/appBg
这种写法代替

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/appBg"
    tools:context="com.example.song.changestyle.MainActivity">

    <TextView
        android:textSize="20sp"
        android:id="@+id/theme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shapee"
        android:text="Hello World!"
        android:textColor="?attr/textNight"/>
</RelativeLayout>


shapee.xml

需要加上版本控制, drawable 中只有在版本v21 以上才好用

所以 在新建xml 时候Directory name 里面应该加上版本控制  drawable-v21 或者点击Version 填入21

?代表多个可选值

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="?attr/appBg"/>
    <corners android:radius="2dp" />
    <stroke android:color="?attr/textLight" android:width="3dp"/>
</shape>



tips

小技巧, 在设置页面设置后返回mainAty

intent  回去, 然后finish();
overridePendindTransition(0,0);

注意MainAty  在清单文件 需要设置为   android:launchMode="singleTask"






低版本有什么好的解决办法, 欢迎大家留言一起探讨~~~




猜你喜欢

转载自blog.csdn.net/qq_36029400/article/details/76919193