colorprimaryandroid
Ⅰ Android QMUI實戰:實現APP換膚功能,並自動適配手機深色模式
要在Android應用中使用QMUI庫實現APP換膚功能,並自動適配手機深色模式,可以按照以下步驟進行:
基礎設置與集成:
- 創建一個新的Android項目。
- 添加QMUI庫的依賴。
- 定義核心屬性,如colorPrimary,colorBg13等,為不同的皮膚主題和默認主題創建相應的styles。
- 在colors.xml文件中配置顏色值。
樣式繼承:
- 利用QMUI的樣式繼承功能,為不同的皮膚主題創建基於基礎主題的擴展。
- 創建一個QDSkinManager管理類,負責載入和切換皮膚。
皮膚持久化與深色模式感知:
- 使用QDPreferenceManager保存皮膚索引,以便下次啟動時恢復。
- 確保QDSkinManager能夠檢測設備的深色模式,並在需要時切換到預設的深色皮膚。
應用啟動與自定義Application:
- 在自定義的Application類中初始化QDSkinManager,並適應深色模式。
- 在AndroidManifest.xml中指定應用的啟動類。
主活動布局與皮膚應用:
- 在主活動的布局文件中使用QMUI的換膚屬性來控制控制項的外觀。
- 在代碼中注冊QMUISkinManager。
QMUI的換膚功能擴展:
- 利用QMUI提供的輔助工具來擴展和優化換膚功能。
- 這些工具可以幫助您更靈活地配置和管理皮膚的切換和應用。
重點: QDSkinManager 是實現換膚功能的核心組件,負責載入和切換皮膚。 QDPreferenceManager 用於保存和恢復皮膚設置,確保用戶選擇的皮膚在下次啟動時仍然有效。 自定義Application 類是初始化和管理換膚功能的關鍵位置。 布局文件中的QMUI換膚屬性 是實現控制項皮膚切換的直接方式。
通過以上步驟,您可以在Android應用中實現強大的換膚功能和深色模式適配能力,為用戶提供個性化的視覺體驗。
Ⅱ Android的supportV7中默認按鈕的顏色設置
我們知道,在styles.xml文件裡面可以設置主題,在主題中設置的一些顏色,將會應用到默認的AppCompat控制項上,從而很簡單的就可以保持整個APP在UI上的一致性。下面是一個例子:
至於各種控制項是如何應用這些顏色設置的,則需要經過更多的嘗試了。
比如Activity導航欄默認的圖標顏色是colorControlNormal,導航欄的底色是colorPrimary,沉浸式狀態欄默認的顏色是colorPrimaryDark;
比如FAB的默認顏色是colorAccent;
比如AppCompatCheckBox默認的選中狀態的顏色是colorAccent,而默認的未選擇狀態的顏色的colorControlNormal;
比如AppCompatSpinner的下拉圖標的默認顏色也是colorControlNormal。
......
其實涉及到的主要的就是下面這幾個參數:
那麼問題來了,如果你使用藍色的沉浸式狀態欄,導航欄上的圖標則使用白色,那在這個Activity中使用AppCompatCheckBox的時候,未選擇狀態就也是白色的,此時如果在白色的背景色下,用戶就看不出這是個AppCompatCheckBox了。這時候怎麼辦?如下圖(圖中使用的是AppCompatSpinner):
其實很簡單,在這個AppCompatCheckBox上使用app:theme="@style/MyCheckBox",然後在styles.xml中添加新的
但是需要注意的是,這樣可能引起控制項其他默認屬性的變化,比如CheckBox的textSize會變成1(不使用app:theme的時候和APP的默認字體大小一樣)。
android:theme和app:popupTheme的作用,以及在android 3.0以下不起作用問題的解決
Ⅲ android 沉浸式狀態欄和透明狀態欄的區別
注意!兩種方法的區別:
第一種:為頂部欄跟隨當前activity的布局文件的背景的顏色,使用方便,不過也有點問題就是,如果有底部虛擬導航鍵的話,導航鍵的背景跟頂部的顏色一樣,比如:
第二種:是通過設置頂部欄的顏色來顯示的,可以解決第一種的不足,比如:
第一種使用方法:
第一、首先在values、values-v19、values-v21文件夾下的styles.xml都設置一個 Translucent System Bar 風格的Theme,如下圖:
values/style.xml:
<style name="TranslucentTheme" parent="AppTheme">
<!--在Android 4.4之前的版本上運行,直接跟隨系統主題-->
</style>123
values-v19/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>1234
values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x開始需要把顏色設置透明,否則導航欄會呈現系統默認的淺灰色-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>123456
第二、在清單文件中配置需要沉浸式狀態欄的activity加入theme
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" />
<activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />12
第三、在Activity的布局文件中的跟布局加入「android:fitsSystemWindows=」true」」,但是,這里需要區分一下,就是背景是圖片還是純色:
1.當背景為圖片時,布局可以這么寫:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/imgs_bj"
android:fitsSystemWindows="true">
</RelativeLayout>12345678
效果:
2.當背景為純色,我們需要對布局劃分一下,標題布局與內容布局,先把根布局背景設置成標題布局的背景色,然後標題背景色可以不用設置直接使用根布局的背景色,最後內容布局背景色設置為白色
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary" //根布局背景設置成「標題布局」想要的顏色
android:fitsSystemWindows="true"
android:orientation="vertical">
<!--標題布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@color/color_31c27c">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="這是標題"
android:textColor="@android:color/white"
android:textSize="20sp" />
</RelativeLayout>
<!--內容布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" //內容區域背景設置成白色
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_marginTop="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="顯示信息"
android:onClick="showMsg"
/>
</LinearLayout>
</LinearLayout>
Ⅳ Android中attr/colorOnPrimary的使用
?attr/colorOnPrimary是一個預定義的顏色屬性,用在Android開發中。它在主題文件中預先設置了一個顏色值,方便開發者在應用中引用。
在Theme.Sunflower主題文件中,?attr/colorOnPrimary對應的顏色值被定義為@color/sunflower_yellow_500。這個顏色值可以被應用於需要顏色的任何地方。例如,當我們將android:fillColor="?attr/colorOnPrimary"設置在SVG圖片的填充屬性上時,圖片的填充顏色會根據應用的主色調自動調整。
在Java代碼中,我們同樣可以通過引用?attr/colorOnPrimary來獲取對應的顏色值,從而應用於需要顏色的控制項或者布局中。這種方式不僅簡化了顏色管理,也使得應用的視覺風格更加統一。
Ⅳ ConstraintLayout中屬性layout_constraintDimensionRatio的使用
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="h,2:1"
android:background="@color/colorPrimary"/>
</androidx.constraintlayout.widget.ConstraintLayout>
當app:layout_constraintDimensionRatio=「h,2:1」 中寫了h的情況下代表高寬比 2 : 1
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="w,1:2"
android:background="@color/colorPrimary"/>
</androidx.constraintlayout.widget.ConstraintLayout>
當app:layout_constraintDimensionRatio=「w,1:2」 中寫了w的情況下代表寬高比1 : 2