日夜间模式切换第一种方法

//首先在工程中自带的values文件夹下 colors中添加颜色

<?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="background">#252a2e</color>
    <color name="unablebtn">#dcdcdc</color>
    <color name="dark_bg">#505050</color>
    <color name="light">#ECECEC</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="green">#05D992</color>
    <color name="zise">#E5004F</color>
    <color name="dark_bg1">#414141</color>
    <color name="pink">#FF5877</color>
    <color name="yellow">#FFFF00</color>
</resources>

//在工程中自带的values文件夹下 strings文件中  自定义

<resources>
    <string name="app_name">DayNightOne</string>
    <string name="changge_to_night">切换成夜间模式</string>
    <string name="changge_to_day">切换成日间模式</string>
</resources>

//创建一个attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="textColorValue" format="color"></attr>
    <attr name="textContent" format="string"></attr>
</resources>


//在工程中自带的values文件夹下 styles文件中  自定义

<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/changge_to_night</item>

    </style>

    <!-- Base application theme. 夜晚的模式 -->
    <style name="night_theme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/dark_bg</item>
        <item name="colorPrimaryDark">@color/dark_bg</item>
        <item name="colorAccent">@color/dark_bg</item>
        <item name="android:windowBackground">@color/dark_bg</item>

        <!--夜间模式对应的字体颜色 和夜间模式对应的文本内容-->
        <item name="textColorValue">@color/white</item>
        <item name="textContent">@string/changge_to_day</item>
    </style>

</resources>


//res下创建一个文件夹anim设置切换时的动画  创建两个xml文件

//第一个sliding_in.xml

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

//第二个sliding_out.xml

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


//在清单文件中要更改配置

android:theme="@style/day_theme"

//创建一个类

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 onActivityCreatedSetTheme(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()));
    }
}

//MainActivity中

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        //设置对应的主题 ,在ui创建好之后设置主题无效,所以要放到setContentView()方法前面setTheme()
        ThemeUtil.onActivityCreatedSetTheme(this);
        setContentView(R.layout.activity_main);
    }


    public void onClick(View v) {

        //切换日夜间模式
        ThemeUtil.ChangeCurrentTheme(this);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值