完整版的设置夜间模式

activity_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:gravity="center_horizontal"
    android:orientation="vertical"

    >

    <Button
        android:id="@+id/change_theme_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:onClick="onClick"
        android:text="?attr/textContent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="Hello World!"
        android:textColor="?attr/textColorValue" />

    <TextView
        android:textColor="?attr/textColorValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Hello World!" />

    <TextView
        android:textColor="?attr/textColorValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Hello World!" />

</LinearLayout>

res下面的anim动画包sliding_in

<?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 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>

values下的atts

<?xml version="1.0" encoding="utf-8" ?>

<resources>
    <attr name="textColorValue" format="color"></attr>
    <attr name="textContent" format="string"></attr>


</resources>
设置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="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>

strings.xml

<resources>
    <string name="app_name">Wuhaodaynight</string>
    <string name="changge_to_night">切换成夜间模式</string>
    <string name="changee_to_day">切換成日間模式</string>

</resources>
styles.xml

<resources>

    <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/changee_to_day</item>
    </style>


</resources>
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 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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.onActivityCreatedSetTheme(this);
        setContentView(R.layout.activity_main);




    }
    public void onClick(View v){
        //切換為日夜間模式
        ThemeUtil.ChangeCurrentTheme(this);


    }


}
改变清单文件的theme文件

android:theme="@style/day_theme"


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值