亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

AndroidQ(10)黑暗模式適配的實(shí)現(xiàn)

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

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

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

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

AndroidQ黑暗模式適配:

適配原理介紹:黑暗模式和正常模式,無非就是兩種主題間的切換(主要是各種背景色,字體顏色和Icon)。因此我們只需要定義兩套不同的主題,根據(jù)是否是黑暗模式進(jìn)行主題的切換即可。

詳細(xì)步驟:

判斷當(dāng)前是否處于黑暗模式:用于啟動時還在不同的主題

 //檢查當(dāng)前系統(tǒng)是否已開啟暗黑模式
  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()方法前設(shè)置:
 @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)
   }

//為達(dá)到更好的適配效果,可在xml文件的activity節(jié)點(diǎn)下加入如下屬性:
android:configChanges="uiMode"

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

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

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

相關(guān)文章

  • Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例(示例四)

    Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例(示例四)

    本篇文章主要介紹了Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例,具有一定的參考價值,需要的朋友可以了解一下。
    2016-12-12
  • 最新評論