Androidステータスバーの背景色とアイコンの色の変更の概要



Android Status Bar Background Color



ステータスバーの背景とアイコンの色を変更するには、いくつかの方法があります。

注:api 23はステータスバーアイコンの色の変更のみをサポートしているため、アイコンの色の変更はコンパイルapi> = 23で実装されます
Android 4.4より前のバージョンでは、ステータスバーはデフォルトで黒になっており、変更できません。
2つのAndroid4.4は、ステータスバーを透明にするかどうかを設定できます
3つのAndroid5.0 +は、ステータスバーの背景をアクティブに設定できます



1ダイレクトコードはステータスバーの色とアイコンの色を変更します
apiが23を超える携帯電話のコード行は解決できますStatusBarUtils.setStatusBar(getResources()。getColor(R.color.text_white)、MainActivity.this)

2 android:fitsSystemWindowsを使用する
6.2を表示
3パディングトップの使用
api> = 19の場合、ステータスバーを透明に設定してから、ルートレイアウトでpaddingtop = 25dpを設定して、ステータスバーの背景色とタイトルバーの色を同じにします。 android:fitsSystemWindowsエフェクトを使用します。この方法で使用します
すべてのルートレイアウトで使用する必要があり、使用するのが面倒です。推奨されません。



4仕上げXiaomiとMeizuの携帯電話のステータスバー互換プログラム

/** * Status bar bright color mode, set the status bar black text, icon, * Adapted to 4.4 or above versions of MIUIV, Flyme and 6.0 or above for other Android * @param activity * @return 1:MIUUI 2:Flyme 3:android6.0 */ public static int StatusBarLightMode(Activity activity){ int result=0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if(MIUISetStatusBarLightMode(activity, true)){ result=1 }else if(FlymeSetStatusBarLightMode(activity.getWindow(), true)){ result=2 }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN } return result } /** * When the system type is known, set the status bar black text and icon. * Adapted to 4.4 or above versions of MIUIV, Flyme and 6.0 or above for other Android * @param activity * @param type 1:MIUUI 2:Flyme 3:android6.0 */ public static void StatusBarLightMode(Activity activity,int type){ if(type==1){ MIUISetStatusBarLightMode(activity, true) }else if(type==2){ FlymeSetStatusBarLightMode(activity.getWindow(), true) }else if(type==3)View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) } /** * Status bar dark mode, clear MIUI, flyme or 6.0 or above status bar black text, icon */ public static void StatusBarDarkMode(Activity activity,int type){ if(type==1){ MIUISetStatusBarLightMode(activity, false) }else if(type==2){ FlymeSetStatusBarLightMode(activity.getWindow(), false) }else if(type==3){ activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE) } } /** * Set the status bar icons to dark and Meizu specific text styles * Can be used to determine if it is a Flyme user * @param window window to be set * @param dark Is the status bar text and icon color set to dark * @return boolean returns true if successful execution * */ public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) { boolean result = false if (window != null) { try { WindowManager.LayoutParams lp = window.getAttributes() Field darkFlag = WindowManager.LayoutParams.class .getDeclaredField('MEIZU_FLAG_DARK_STATUS_BAR_ICON') Field meizuFlags = WindowManager.LayoutParams.class .getDeclaredField('meizuFlags') darkFlag.setAccessible(true) meizuFlags.setAccessible(true) int bit = darkFlag.getInt(null) int value = meizuFlags.getInt(lp) if (dark) value else { value &= ~bit } meizuFlags.setInt(lp, value) window.setAttributes(lp) result = true } catch (Exception e) { } } return result } /** * Requires MIUIV6 or above * @param activity * @param dark Is the status bar text and icon color set to dark * @return boolean returns true if successful execution * */ public static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) { boolean result = false Window window=activity.getWindow() if (window != null) { Class clazz = window.getClass() try { int darkModeFlag = 0 Class layoutParams = Class.forName('android.view.MiuiWindowManager$LayoutParams') Field field = layoutParams.getField('EXTRA_FLAG_STATUS_BAR_DARK_MODE') darkModeFlag = field.getInt(layoutParams) Method extraFlagField = clazz.getMethod('setExtraFlags', int.class, int.class) if(dark){ extraFlagField.invoke(window,darkModeFlag,darkModeFlag)//Status bar transparent and black font }else{ extraFlagField.invoke(window, 0, darkModeFlag)//Clear black font } result=true if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //Development version 7.7.13 and later uses the system API, the old method is invalid but will not report an error, so both methods must be added if(dark) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) else { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE) } } }catch (Exception e){ } } return result }

523以上21以上23未満のAPIの分析

colorフォルダーのcolorPrimaryDarkの色を変更するだけです(または1のスキームを参照して効果を得る)が、アイコンのフォントの色は変更できません。
はい、グレーに変更することは可能です(カスタムカラーなし)が、これはAPI 23以降でのみ機能し、values-v23 /styles.xmlに追加するだけで済みます。
true

621以上19以上21未満のAPIの分析
ノート:
6.1テーマはフルスクリーンです。デフォルトではフルスクリーンプロパティが表示されないため、ステータスバーは変更されません。

6.2テーマでタイトルバーが使用されている場合、タイトルバーが追加され、ソリューションandroid:fitsSystemWindows =“ true”
またはこれを設定します
ViewGroup mContentView =(ViewGroup)window.findViewById(Window.ID_ANDROID_CONTENT)
ビューmChildView = mContentView.getChildAt(0)
if(mChildView!= null){
// ContentViewにFitsSystemWindowsを設定する代わりに、ContentViewの最初の子ビューを設定することに注意してください。システムビュー用にスペースを予約します。
mChildView.setFitsSystemWindows(fitSystemWindows)
}

7フルスクリーンに適応します。

7.1
したがって、開発者は最大アスペクト比> 2以上を宣言することをお勧めします。アプリケーションのandroid:resizeableActivityがtrueに設定されている場合は、最大アスペクト比を設定する必要はありません。詳細については、公式ドキュメント「最大アスペクト比の宣言」を参照してください。
http://www.easemob.com/news/973

7.2包括的な画面の紹介
https://developer.huawei.com/consumer/cn/devservice/doc/50111
7.2.1全画面定義
通常の画面:アスペクト比は16:9(1080x1920、1440x2560など)です。比率は1.78です。フルスクリーン携帯電話が登場する前は、Androidのデフォルトの最大アスペクト比。これは1.86で、16:9画面と互換性があります。

全画面表示:17:9、18:9、19:9、19.5:9など、画面比率が1.86を超える画面。これらの画面は全画面表示です。

7.2.2フルスクリーンモデル
モデル画面のアスペクト比ratio_float
Mate10プロ18:9 2.0
LG G6 18:9 2.0
サムスンギャラクシーS818.5:9 2.06
Essential Phone 19:10 1.9
Xiaomi MIX
17:9 1.89
iPhone X 19.5:9 2.17

8
Huawei携帯電話の下部にある仮想ナビゲーションバーオクルージョンレイアウトソリューション
8.1仮想ナビゲーションがある場合にレイアウトの高さを変更する
activity.getWindow()。getDecorView()。findViewById(android.R.id.content).setPadding(0、0、0、getNavigationBarHeight(activity))

public static int getNavigationBarHeight(Context context){
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey()
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
if(!hasMenuKey &&!hasBackKey){
リソースresources = context.getResources()
int resourceId = resources.getIdentifier( 'navigation_bar_height'、 'dimen'、 'android')// NavigationBarの高さを取得します
int height = resources.getDimensionPixelSize(resourceId)
戻り高さ
} そうしないと {
0を返す
}
}

8.2
下部のステータスバーが透明になりますが、これによりナビゲーションバーのアイコンが白になります(美しくありません)
getActivity()。getWindow()。addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)