这篇文章主要介绍了AndroidQ(10)黑暗模式适配的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
前言:作为一个Android程序员,每年最期待就是Google的发布会啦!!这不,今年的AndroidQ如期而至。这里简单介绍一下Android的新特性:
- AndroidQ全局暗黑模式
- 隐私权限的更新
- AndroidQ新版的手势导航(其实就是仿IOS)
- 系统日程UI的优化(还有其他系统UI上的优化)
- Google组件(jetpack)的推荐
每年的Google大会一结束就是程序员忙碌工作的开端,各种适配,各种新功能… 一堆事情下来,搞的焦头烂额。 但是今年的发布会之后,仔细一看Q的更新清单,其实需要我们去适配优化的并不多,主要就是隐私权限和黑暗模式需要我们紧急适配。而且黑暗模式和以往的多主题适配是一个道理,这样我们的跟进优化工作就更加简单了。废话不多说,这里我们就来介绍一下在原生系统下进行黑暗模式的适配。
AndroidQ黑暗模式适配:
适配原理介绍:黑暗模式和正常模式,无非就是两种主题间的切换(主要是各种背景色,字体颜色和Icon)。因此我们只需要定义两套不同的主题,根据是否是黑暗模式进行主题的切换即可。
详细步骤:
判断当前是否处于黑暗模式:用于启动时还在不同的主题
//检查当前系统是否已开启暗黑模式
public static boolean getDarkModeStatus(Context context) {
int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
return mode == Configuration.UI_MODE_NIGHT_YES;
}
定义两套主题(正常模式和黑暗模式):即在style文件下自定义两个style,但是必须指定parent为‘Theme.AppCompat.DayNight.DarkActionBar',如下所示:
//正常模式下的主题
<style name="main_theme_light" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="main_text_color">@color/main_text_color_light</item>
<item name="main_bg_color">@color/main_bg_color_light</item>
</style>
//黑暗模式下的主题
<style name="main_theme_dark" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="main_text_color">@color/main_text_color_dark</item>
<item name="main_bg_color">@color/main_bg_color_dark</item>
</style>
找出适配黑暗模式需要的属性(主要是颜色属性:背景色、字体颜色和Icon颜色等并给属性赋值),类似如下定义:
供在上一步的style中引用,不同模式下提供不同的值
<!-- 主要字体颜色-->
<attr name="main_text_color" format="color" />
<!-- 主要背景颜色-->
<attr name="main_bg_color" format="color" />
//不同模式下的颜色属性值
<color name="main_text_color_light">#000000</color>
<color name="main_text_color_dark">#ffffff</color>
<color name="main_bg_color_light">#ffffff</color>
<color name="main_bg_color_dark">#000000</color>
在activity和xml中引用我们自定义的属性:
//在xml文件中使用我们自定义属性
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/main_bg_color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="?attr/main_text_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//在BaseActivity中切换不同的主题,才能使我们自定义的属性生效,必须在setContentView()方法前设置:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (getDarkModeStatus(this)) {
setTheme(R.style.main_theme_dark);
}else {
setTheme(R.style.main_theme_light);
}
setContentView(R.layout.activity_main)
}
//为达到更好的适配效果,可在xml文件的activity节点下加入如下属性:
android:configChanges="uiMode"
ps:Icon的适配可以借助tint属性切换不同模式的颜色。
总结:到此为止,我们在两个模式下的切换就算完成了,你可以尝试开启系统的黑暗模式,可见我们的几面也会换成黑暗模式下的主题。如果有更多不同主题,那我们的工作就简单了,只需要在style文件下增加主题,并且加入主题下的颜色值就可以了。
到此这篇关于AndroidQ(10)黑暗模式适配的实现的文章就介绍到这了,更多相关AndroidQ(10)黑暗模式内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:AndroidQ(10)黑暗模式适配的实现


- Android实现轮询的三种方式 2023-02-17
- Android studio实现动态背景页面 2023-05-23
- Flutter实现底部和顶部导航栏 2022-08-31
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android实现监听音量的变化 2023-03-30
- 详解flutter engine 那些没被释放的东西 2022-12-04
- iOS 对当前webView进行截屏的方法 2023-03-01
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02