最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AndroidQ(10)黑暗模式適配的實現

 更新時間:2020年06月23日 11:22:04   作者:legendCoder  
這篇文章主要介紹了AndroidQ(10)黑暗模式適配的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言:作為一個Android程序員,每年最期待就是Google的發(fā)布會啦??!這不,今年的AndroidQ如期而至。這里簡單介紹一下Android的新特性:

  • AndroidQ全局暗黑模式
  • 隱私權限的更新
  • AndroidQ新版的手勢導航(其實就是仿IOS)
  • 系統日程UI的優(yōu)化(還有其他系統UI上的優(yōu)化)
  • Google組件(jetpack)的推薦

每年的Google大會一結束就是程序員忙碌工作的開端,各種適配,各種新功能… 一堆事情下來,搞的焦頭爛額。 但是今年的發(fā)布會之后,仔細一看Q的更新清單,其實需要我們去適配優(yōu)化的并不多,主要就是隱私權限和黑暗模式需要我們緊急適配。而且黑暗模式和以往的多主題適配是一個道理,這樣我們的跟進優(yōu)化工作就更加簡單了。廢話不多說,這里我們就來介紹一下在原生系統下進行黑暗模式的適配。

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節(jié)點下加入如下屬性:
android:configChanges="uiMode"

ps:Icon的適配可以借助tint屬性切換不同模式的顏色。

總結:到此為止,我們在兩個模式下的切換就算完成了,你可以嘗試開啟系統的黑暗模式,可見我們的幾面也會換成黑暗模式下的主題。如果有更多不同主題,那我們的工作就簡單了,只需要在style文件下增加主題,并且加入主題下的顏色值就可以了。

到此這篇關于AndroidQ(10)黑暗模式適配的實現的文章就介紹到這了,更多相關AndroidQ(10)黑暗模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

五寨县| 册亨县| 元谋县| 临高县| 铜陵市| 西峡县| 铜川市| 西充县| 石泉县| 高阳县| 大宁县| 沙雅县| 舒兰市| 湖南省| 高要市| 灌阳县| 溆浦县| 通城县| 瑞丽市| 杨浦区| 仁化县| 平昌县| 托克逊县| 辉县市| 珠海市| 新泰市| 临武县| 泸西县| 时尚| 徐州市| 绥化市| 安溪县| 清涧县| 中牟县| 古交市| 舟曲县| 黄梅县| 锦州市| 芒康县| 富川| 班玛县|