AndroidQ(10)黑暗模式適配的實現(xiàn)
前言:作為一個Android程序員,每年最期待就是Google的發(fā)布會啦??!這不,今年的AndroidQ如期而至。這里簡單介紹一下Android的新特性:
- AndroidQ全局暗黑模式
- 隱私權限的更新
- AndroidQ新版的手勢導航(其實就是仿IOS)
- 系統(tǒng)日程UI的優(yōu)化(還有其他系統(tǒng)UI上的優(yōu)化)
- Google組件(jetpack)的推薦
每年的Google大會一結束就是程序員忙碌工作的開端,各種適配,各種新功能… 一堆事情下來,搞的焦頭爛額。 但是今年的發(fā)布會之后,仔細一看Q的更新清單,其實需要我們?nèi)ミm配優(yōu)化的并不多,主要就是隱私權限和黑暗模式需要我們緊急適配。而且黑暗模式和以往的多主題適配是一個道理,這樣我們的跟進優(yōu)化工作就更加簡單了。廢話不多說,這里我們就來介紹一下在原生系統(tǒng)下進行黑暗模式的適配。
AndroidQ黑暗模式適配:
適配原理介紹:黑暗模式和正常模式,無非就是兩種主題間的切換(主要是各種背景色,字體顏色和Icon)。因此我們只需要定義兩套不同的主題,根據(jù)是否是黑暗模式進行主題的切換即可。
詳細步驟:
判斷當前是否處于黑暗模式:用于啟動時還在不同的主題
//檢查當前系統(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()方法前設置: @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屬性切換不同模式的顏色。
總結:到此為止,我們在兩個模式下的切換就算完成了,你可以嘗試開啟系統(tǒng)的黑暗模式,可見我們的幾面也會換成黑暗模式下的主題。如果有更多不同主題,那我們的工作就簡單了,只需要在style文件下增加主題,并且加入主題下的顏色值就可以了。
到此這篇關于AndroidQ(10)黑暗模式適配的實現(xiàn)的文章就介紹到這了,更多相關AndroidQ(10)黑暗模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
android 更改TextView中任意位置字體大小和顏色的方法
下面小編就為大家分享一篇android 更改TextView中任意位置字體大小和顏色的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android最簡單的限制輸入方法(只包含數(shù)字、字母和符號)
這篇文章主要給大家介紹了關于Android最簡單的限制輸入的實現(xiàn)方法,限制輸入框只能輸入數(shù)字、字母和符號,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看 吧2018-11-11React-Native之Android(6.0及以上)權限申請詳解
這篇文章主要介紹了React-Native之Android(6.0及以上)權限申請詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Android種使用Notification實現(xiàn)通知管理以及自定義通知欄實例(示例四)
本篇文章主要介紹了Android種使用Notification實現(xiàn)通知管理以及自定義通知欄實例,具有一定的參考價值,需要的朋友可以了解一下。2016-12-12