zoukankan      html  css  js  c++  java
  • android夜间模式实现

    一、概述

    android夜间模式实现分为两大类

    • 重启activity的实现
    • 不重启activity的实现

    二、正文

    1、重启activity实现夜间模式【在界面文件中的实现部分】

    1.1在attrs.xml文件中定义如下属性

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!--Activity-->
        <attr name="rootViewBgColor" format="reference|color"/>
        <attr name="buttonBgColor" format="reference|color"/>
        <attr name="defaultTextColor" format="reference|color"/>
        <!--CardView-->
        <attr name="cardViewBgColor" format="reference|color"/>
        <attr name="cardViewTextTitleColor" format="reference|color"/>
        <attr name="cardViewTextBodyColor" format="reference|color"/>
        <attr name="cardViewTextHightLightColor" format="reference|color"/>
        <attr name="cardViewTextExtraColor" format="reference|color"/>
    </resources>
    

    1.2在stytles.xml文件中定义如下属性

    <style name="DayTimeModeTheme" parent="AppTheme.NoActionBar">
            <item name="cardViewBgColor">#0CB8F6</item>
            <item name="cardViewTextTitleColor">#DBDBDB</item>
    </style>
    <style name="NightTimeModeTheme" parent="AppTheme.NoActionBar">
        <item name="colorPrimary">@color/darken</item>
        <item name="colorPrimaryDark">@color/black</item>
        <item name="colorAccent">@color/dark</item>
    
        <item name="android:textColor">#DBDBDB</item>
        <item name="android:textColorHighlight">#F9E81E</item>
        <item name="android:textColorLink">#242F67</item>
        <item name="android:textColorHint">#DBDBDB</item>
    
        <item name="cardViewBgColor">#18255D</item>
        <item name="cardViewTextTitleColor">#61809E</item>
    </style>
    

    1.3在所有的界面文件中使用如下代码(以cardView为例)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    <android.support.v7.widget.CardView
            android:id="@+id/cv_news"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            app:cardBackgroundColor="?attr/cardViewBgColor"//这里使用了attr中自定义的属性值
            app:cardCornerRadius="10dp"
            app:cardElevation="5dp"
            app:contentPadding="10dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="标题"
                    android:textColor="?attr/cardViewTextTitleColor"//这里使用了attr中自定义的属性值
                    android:textSize="14sp"/>
                <TextView
                    android:id="@+id/tv_body"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="正文正文正文正文正文正文"
                    android:textColor="?attr/cardViewTextTitleColor"//这里使用了attr中自定义的属性值
                    android:textSize="12sp"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
    

    2、重启activity实现夜间模式【在java代码中的实现部分】

    2.1使用SharedPreference当前存储主题状态

    博主自定义了SharedPreferrenceHelper类对“主题存储、主题获取、主题切换”的SharedPreference操作进行了封装

    package com.neil.dailyzhihu.ui.theme;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    import com.neil.dailyzhihu.Constant;
    import com.neil.dailyzhihu.R;
    
    public class SharedPreferrenceHelper {
    
        public static void settheme(Context context, String theme) {
            SharedPreferences sp = context.getSharedPreferences(Constant.SHARED_PREFERANCE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(Constant.DAY_MODE_THEME, theme);
            editor.commit();
        }
    
        public static String gettheme(Context context) {
            SharedPreferences sp = context.getSharedPreferences(Constant.SHARED_PREFERANCE_NAME, Context.MODE_PRIVATE);
            return sp.getString(Constant.DAY_MODE_THEME, Constant.DEFAULT_TIME_THEME);
        }
    
        public static void switchAppTheme(Context context) {
            String value = SharedPreferrenceHelper.gettheme(context);
            switch (value) {
                case Constant.NIGHT_TIME_THEME:
                    SharedPreferrenceHelper.settheme(context, Constant.DAY_TIME_THEME);
                    break;
                case Constant.DAY_TIME_THEME:
                    SharedPreferrenceHelper.settheme(context, Constant.NIGHT_TIME_THEME);
                    break;
            }
        }
    
        public static int getAppTheme(Context context) {
            String value = SharedPreferrenceHelper.gettheme(context);
            switch (value) {
                case Constant.NIGHT_TIME_THEME:
                    return R.style.DayTimeModeTheme;
                case Constant.DAY_TIME_THEME:
                    return R.style.NightTimeModeTheme;
            }
            return R.style.DayTimeModeTheme;
        }
    }
    

    2.2需要切换日夜间模式的activity中的实现

    • 使用夜间模式功能的activity中加入成员变量
    private String theme = Constant.DEFAULT_TIME_THEME;
    
    • onCreate方法中加入如下代码
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int themeRes = SharedPreferrenceHelper.getAppTheme(this);
        theme = SharedPreferrenceHelper.gettheme(this);
        setTheme(themeRes);
        etContentView(R.layout.activity_main);
    }
    
    • activity中添加如下成员方法
    private void reload() {
        Intent intent = getIntent();
        overridePendingTransition(0, 0);//不设置进入退出动画
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }
    
    • 在需要切换日/夜间模式的地方使用reload();方法

    --------------分割线----------------

    3、不重启activity实现夜间模式

    下次更新

  • 相关阅读:
    InnoDB 事务隔离级探索
    套接字 缓冲区 6次拷贝 内核协议栈
    Python Data Structure and Algorithms Tutorial
    任何Python线程执行前,必须先获得GIL锁,然后,每执行100条字节码,解释器就自动释放GIL锁,让别的线程有机会执行
    Linux网络状态工具ss命令使用详解
    不占用额外内存空间能否做到 将图像旋转90度 N &#215; N矩阵表示的图像,其中每个像素的大小为4字节
    尾递归 栈溢出
    t
    t
    __del__ PyPy和CPython的不同点 动态编译(注意不是解释) 析构函数被调用的次数
  • 原文地址:https://www.cnblogs.com/neillee/p/5398301.html
Copyright © 2011-2022 走看看