android 沉浸式之改變小米狀態列顏色
阿新 • • 發佈:2019-02-18
這個是基於SystemBarTintManager更改的
增加一個方法:用於更改MIUIV6系統上的狀態列字型顏色 ,目前我僅僅只發現MIUIV6上可以更改,在android5.0上以及其它4.4以上系統沒有發現可以更改字型顏色的程式碼
核心程式碼:
[java]view plaincopyprint?- publicvoid setStatusBarDarkMode(boolean darkmode, Activity activity) {
- if (sIsMiuiV6) {
-
Class<? extends
- 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);
- extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
全部程式碼: [java]view plaincopyprint?
- /*
- * Copyright (C) 2013 readyState Software Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import android.annotation.SuppressLint;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.Configuration;
- import android.content.res.Resources;
- import android.content.res.TypedArray;
- import android.graphics.drawable.Drawable;
- import android.os.Build;
- import android.util.DisplayMetrics;
- import android.util.TypedValue;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewConfiguration;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.FrameLayout.LayoutParams;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- /**
- * Class to manage status and navigation bar tint effects when using KitKat
- * translucent system UI modes.
- *
- */
- publicclass SystemBarTintManager {
- /**
- * The default system bar tint color value.
- */
- publicstaticfinalint DEFAULT_TINT_COLOR = 0x99000000;
- privatestatic String sNavBarOverride;
- privatefinal SystemBarConfig mConfig;
- privateboolean mStatusBarAvailable;
- privateboolean mNavBarAvailable;
- privateboolean mStatusBarTintEnabled;
- privateboolean mNavBarTintEnabled;
- private View mStatusBarTintView;
- private View mNavBarTintView;
- privatestaticboolean sIsMiuiV6;
- static {
- // Android allows a system property to override the presence of the navigation bar.
- // Used by the emulator.
- // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- try {
- Class c = Class.forName("android.os.SystemProperties");
- Method m = c.getDeclaredMethod("get", String.class);
- m.setAccessible(true);
- sIsMiuiV6 = "V6".equals((String) m.invoke(c, "ro.miui.ui.version.name"));
- sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
- } catch (Throwable e) {
- sNavBarOverride = null;
- }
- }
- }
- /**
- * Constructor. Call this in the host activity onCreate method after its
- * content view has been set. You should always create new instances when
- * the host activity is recreated.
- *
- * @param activity The host activity.
- */
- @TargetApi(19)
- public SystemBarTintManager(Activity activity) {
- Window win = activity.getWindow();
- ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- // check theme attrs
- int[] attrs = {android.R.attr.windowTranslucentStatus,
- android.R.attr.windowTranslucentNavigation};
- TypedArray a = activity.obtainStyledAttributes(attrs);
- try {
- mStatusBarAvailable = a.getBoolean(0, false);
- mNavBarAvailable = a.getBoolean(1, false);
- } finally {
- a.recycle();
- }
- // check window flags
- WindowManager.LayoutParams winParams = win.getAttributes();
- int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
- if ((winParams.flags & bits) != 0) {
- mStatusBarAvailable = true;
- }
- bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
- if ((winParams.flags & bits) != 0) {
- mNavBarAvailable = true;
- }
- }
- mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable);