DayNight实现日夜间模式

这篇博客介绍了两种在Android中实现DayNight日夜间模式切换的方法。第一种方法通过示例图展示日间和夜间模式的效果,并给出了主界面布局及相关代码。第二种方法则通过在values目录下创建日间和夜间模式的资源文件来实现切换。
摘要由CSDN通过智能技术生成

有两种方式进行 切换日夜间模式

第一种

先上示例图

                    日间模式                                                                               夜间模式


主界面布局:


<?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:orientation="vertical"
    >

    <Button
        android:id="@+id/change_theme_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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!xinbaba"
        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!lixin" />

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


</LinearLayout>
Values


arrts(文件中定义了控件的所有属性以及所用控件的声明

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

</resources>
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>
strings(当中定义使用到的字符串常量。)

<resources>
    <string name="app_name">DayNightOne</string>
    <string name="change_to_night">
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值