| | |
| | | import android.app.Activity; |
| | | import android.graphics.Color; |
| | | import android.os.Build; |
| | | import android.os.Environment; |
| | | import android.text.TextUtils; |
| | | import android.view.View; |
| | | import android.view.ViewGroup; |
| | | import android.view.Window; |
| | | import android.view.WindowManager; |
| | | import android.widget.LinearLayout; |
| | |
| | | |
| | | import com.tejia.lijin.app.R; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Method; |
| | | import java.util.Properties; |
| | | |
| | | /** |
| | | * Created by weikou2015 on 2018/9/13. |
| | |
| | | } |
| | | //设置状态栏文字颜色及图标为深色 |
| | | mActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| | | RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, result); |
| | | mActivity.findViewById(R.id.v_status_bar).setLayoutParams(params); |
| | | ViewGroup.LayoutParams params=mActivity.findViewById(R.id.v_status_bar).getLayoutParams(); |
| | | params.width=ViewGroup.LayoutParams.MATCH_PARENT; |
| | | params.height=result; |
| | | // RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, result); |
| | | // if( mActivity.findViewById(R.id.v_status_bar).getLayoutParams() instanceof RelativeLayout.LayoutParams) { |
| | | // mActivity.findViewById(R.id.v_status_bar).setLayoutParams(params); |
| | | // } |
| | | try { |
| | | Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView"); |
| | | Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor"); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 修改状态栏文字颜色,这里小米,魅族区别对待。 |
| | | */ |
| | | public static void setLightStatusBar(final Activity activity, final boolean dark) { |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { |
| | | switch (RomUtils.getLightStatusBarAvailableRomType()) { |
| | | case RomUtils.AvailableRomType.MIUI: |
| | | MIUISetStatusBarLightMode(activity, dark); |
| | | break; |
| | | |
| | | case RomUtils.AvailableRomType.FLYME: |
| | | setFlymeLightStatusBar(activity, dark); |
| | | |
| | | break; |
| | | |
| | | case RomUtils.AvailableRomType.ANDROID_NATIVE: |
| | | setAndroidNativeLightStatusBar(activity, dark); |
| | | break; |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | 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);//状态栏透明且黑色字体 |
| | | } else { |
| | | extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体 |
| | | } |
| | | result = true; |
| | | |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && RomUtils.isMiUIV7OrAbove()) { |
| | | //开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上 |
| | | if (dark) { |
| | | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| | | } else { |
| | | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private static boolean setFlymeLightStatusBar(Activity activity, boolean dark) { |
| | | boolean result = false; |
| | | if (activity != null) { |
| | | try { |
| | | WindowManager.LayoutParams lp = activity.getWindow().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 |= bit; |
| | | } else { |
| | | value &= ~bit; |
| | | } |
| | | meizuFlags.setInt(lp, value); |
| | | activity.getWindow().setAttributes(lp); |
| | | result = true; |
| | | } catch (Exception e) { |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) { |
| | | View decor = activity.getWindow().getDecorView(); |
| | | if (dark) { |
| | | decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| | | } else { |
| | | decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| | | } |
| | | } |
| | | |
| | | public static class RomUtils { |
| | | class AvailableRomType { |
| | | public static final int MIUI = 1; |
| | | public static final int FLYME = 2; |
| | | public static final int ANDROID_NATIVE = 3; |
| | | public static final int NA = 4; |
| | | } |
| | | |
| | | public static int getLightStatusBarAvailableRomType() { |
| | | //开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错 |
| | | if (isMiUIV7OrAbove()) { |
| | | return AvailableRomType.ANDROID_NATIVE; |
| | | } |
| | | |
| | | if (isMiUIV6OrAbove()) { |
| | | return AvailableRomType.MIUI; |
| | | } |
| | | |
| | | if (isFlymeV4OrAbove()) { |
| | | return AvailableRomType.FLYME; |
| | | } |
| | | |
| | | if (isAndroidMOrAbove()) { |
| | | return AvailableRomType.ANDROID_NATIVE; |
| | | } |
| | | |
| | | return AvailableRomType.NA; |
| | | } |
| | | |
| | | //Flyme V4的displayId格式为 [Flyme OS 4.x.x.xA] |
| | | //Flyme V5的displayId格式为 [Flyme 5.x.x.x beta] |
| | | private static boolean isFlymeV4OrAbove() { |
| | | String displayId = Build.DISPLAY; |
| | | if (!TextUtils.isEmpty(displayId) && displayId.contains("Flyme")) { |
| | | String[] displayIdArray = displayId.split(" "); |
| | | for (String temp : displayIdArray) { |
| | | //版本号4以上,形如4.x. |
| | | if (temp.matches("^[4-9]\\.(\\d+\\.)+\\S*")) { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | //Android Api 23以上 |
| | | private static boolean isAndroidMOrAbove() { |
| | | return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; |
| | | } |
| | | |
| | | private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code"; |
| | | |
| | | private static boolean isMiUIV6OrAbove() { |
| | | try { |
| | | final Properties properties = new Properties(); |
| | | properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"))); |
| | | String uiCode = properties.getProperty(KEY_MIUI_VERSION_CODE, null); |
| | | if (uiCode != null) { |
| | | int code = Integer.parseInt(uiCode); |
| | | return code >= 4; |
| | | } else { |
| | | return false; |
| | | } |
| | | |
| | | } catch (final Exception e) { |
| | | return false; |
| | | } |
| | | |
| | | } |
| | | |
| | | static boolean isMiUIV7OrAbove() { |
| | | try { |
| | | final Properties properties = new Properties(); |
| | | properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop"))); |
| | | String uiCode = properties.getProperty(KEY_MIUI_VERSION_CODE, null); |
| | | if (uiCode != null) { |
| | | int code = Integer.parseInt(uiCode); |
| | | return code >= 5; |
| | | } else { |
| | | return false; |
| | | } |
| | | |
| | | } catch (final Exception e) { |
| | | return false; |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |