温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现AndroidQ黑暗模式适配?

发布时间:2020-06-23 14:34:34 来源:亿速云 阅读:216 作者:清晨 栏目:开发技术

不懂如何实现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="&#63;attr/main_bg_color">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textColor="&#63;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属性切换不同模式的颜色。

感谢你能够认真阅读完这篇文章,希望小编分享如何实现AndroidQ黑暗模式适配?内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI