Android 实现夜间模式
方法1
建一个 value-night 的文件夹,colors.xml如下:
<resources> <color name="colorPrimary">#7D4112</color> <color name="colorPrimaryDark">#7D4112</color> <color name="colorAccent">#1F1F1F</color></resources>
@Overridepublic boolean onNavigationItemSelected(MenuItem menuItem) { int id = menuItem.getItemId(); switch (id) { case R.id.nav_setting: Toast.makeText(this, "夜间模式", Toast.LENGTH_LONG).show(); if (isNightMode) { uiManager.enableCarMode(0); uiManager.setNightMode(UiModeManager.MODE_NIGHT_YES); isNightMode = !isNightMode; } else { uiManager.disableCarMode(0); uiManager.setNightMode(UiModeManager.MODE_NIGHT_NO); isNightMode = !isNightMode; } return true; default: return true; }}
但是这种方法会开启车载模式
在笔者的测试机(红米1s)中,这种方法会先退出应用,再启动应用已经是夜间模式
注意点
CardView 的背景色是app:cardBackgroundColor="?attr/windowBackground"
设置,不是普通的android:background
更多相关文章
- android读取图片
- Android 使用Glide实现圆形和圆角图片
- 2012-7-20 android 图片叠加效果——两种方法
- Android右滑返回上一个界面的实现方法
- 系统关闭GPRS数据流量和 GPS的方法
- Android添加半透明操作提示的方法
- Android Activity中启动另一应用程序的方法