当下各种阅读类APP(如各种浏览器,某日头条等)都会有夜间模式,也顺应了大家的睡前必须玩一下手机的作息习惯。关于夜间模式的实现,有很多种方法。这篇日志学习一下最简单的实现方式,通过setTheme(int resid)方法实现主题切换来实现夜间模式,这也是Android官方推荐的方法。

整体思路与效果

通过Android SDK提供的setTheme方法,可以切换Activity的不同的主题,这样定义一个合适的主题,就可以实现夜间模式了。

首先看一下效果图

定义不同的主题

自定义主题属性

<resources>    <attr name="textColorValue" format="color" />      ...</resources>

这里我们可以定义一些切换时,需要更改的内容属性。比如说,当前页面中所有文字的颜色,在平时是黑色,在夜间模式是灰色,那么可以在这里定义一个属性,在自定义主题时设置不同的颜色即可。

自定义不同的主题

<!--日间模式主题-->    <style name="CustomThemeLight" parent="Theme.AppCompat.Light.NoActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/light</item>        <item name="colorPrimaryDark">@color/light</item>        <item name="colorAccent">@color/black</item>        <item name="android:textColorPrimary">@color/black</item>        <item name="android:windowBackground">@color/white</item>        <item name="textContent">@string/theme0</item>        <item name="textColorValue">@color/black</item>        <item name="pupwindowTheme">@style/AppTheme.PopupLight</item>    </style>    <!--夜间模式主题-->    <style name="CustomThemeDark" parent="Theme.AppCompat.Light.NoActionBar">        <!-- 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="textContent">@string/theme1</item>        <item name="textColorValue">@color/white</item>        <item name="pupwindowTheme">@style/AppTheme.PopupDark</item>    </style>

熟悉ToolBar的同学,应该对上面几个属性不陌生。这里首先设置了colorPrimary,colorPrimaryDark,windowBackground等几个属性,首先确定了这个主题整体的色调

接下来就上对自定义属性的设置,这里我们可以看到在textColorValue的属性在日间模式和夜间模式中分别设置成了黑色和白色。

自定义属性的使用

接下来,就是将自定义属性作用到布局文件中

<RelativeLayout  android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content">        <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:padding="10dp" android:text="公开文章" android:textColor="?attr/textColorValue" android:textSize="16sp" />        <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:drawablePadding="10dp" android:drawableRight="@mipmap/right_arrow" android:gravity="center_vertical" android:padding="10dp" android:text="11" android:textColor="?attr/textColorValue" />    </RelativeLayout>

如上面这段代码所示,你可以将应用中所有TextView的TextColor的属性设置为?attr/textColorValue,这样他的值就会根据你在自定义主题中所设定的值变化。这样是不是比通过java代码写一大串if-else方便许多呢。

到这里你应该想到了,在自定义属性时可以定义各种各样的内容,TextView的大小,ImageView的大小,LinearLayout的背景等等。虽然这样在一开始写代码的时候有些繁琐,但从长远来说,是很有价值的。

主题切换

这里首先需要明确一点,setTheme方法必须在setContentView方法之前执行,才会有效(这个有点让人失望,但想想也是必然的),所以呢,采用这种方式实现夜间模式,Activity必然需要重启,因此这里需要考虑到Activity切换的问题,不能让人觉得换了一个夜间模式,整个应用重启了一遍,那也太low了。

Activity切换效果

activity.finish();        activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

这里只需定义两个动画效果,确保Activity切换效果不明显即可,采用alpha动画即可,这里就不再赘述。

主题切换实现

 @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mContext = this;        Util.onActivityCreateSetTheme(this);        setContentView(R.layout.activity_main);           }

这里我们在setContentView方法之前调用setTheme方法

主题切换工具类util

    public static void onActivityCreateSetTheme(Activity activity) {        switch (sTheme) {            case Constants.THEME_DARK:               activity.setTheme(R.style.CustomThemeDark);                break;                                       case Constants.THEME_LIGHT:               activity.setTheme(R.style.CustomThemeLight);                break;            default:                break;        }    }

这样就为当前Activity设置了相应的主题。

调用下面的方法,传入不同的主题id即可实现主题切换

 public static void changeToTheme(Activity activity, int theme) {        sTheme = theme;        activity.finish();        activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);        activity.startActivity(new Intent(activity, activity.getClass()));    }

好了,这里就是关于夜间模式的简单了解。这种方式,相对来说比较繁琐,但也最容易理解。当然,这里只是从最简单的角度出发,没有考虑Activity重启前后数据保存和恢复的问题,以及对Fragment的影响等问题,实际问题还是需要根据实际项目分析,这里就不展开来说。

这里对于自定义属性和主题的用法,还是值得借鉴,即便不一定要用到夜间模式,当时通过切换主题实现UI的更新也是一种考虑。

完整代码已上传到github,有兴趣的同学可以参考一下。

github代码地址

更多相关文章

  1. No.11 使用firewall配置的防火墙策略的生效模式
  2. Android中动态显示gif图片
  3. 使用Valgrind找出Android中Native程序内存泄露问题
  4. Android启动模式完全解析(下)
  5. Intent的简介以及属性的详解
  6. 如何设置Android(安卓)系统的属性,Build.prop, defualt.prop
  7. EditText组件drawableLeft属性设置的图片和hint设置的文字之间的
  8. Android(安卓)弹无虚发之第一弹:Android(安卓)2.X平台完美兼容Act
  9. Android设计模式学习之Builder模式

随机推荐

  1. Android中的Intent详解
  2. 【Android】Android背景选择器selector用
  3. Java/Android引用类型及其使用分析
  4. Android Activity切换动画overridePendin
  5. org.json.JSONException: End of input a
  6. Android之使用Pull解析Xml数据
  7. android短信服务
  8. Android 里面的android_secret_code
  9. Android ScrollView包裹EditText 软键盘
  10. 获取应用签名