Android沉浸式状态栏

废话少说 先上效果图 


实现代码

                在onCreate方法中 加入如下代码实现

    //设置状态栏的颜色

StateBarUtil.setStatusBar(this,false, Color.WHITE);

//设置状态栏文字和图标是否使用深色图案

StateBarUtil.setStatusTextColor(true,this);


StateBarUtil工具类

public class StateBarUtil {

/**

* 动态的设置状态栏  实现沉浸式状态栏

*/

    public static void initState(View view, Activity activity) {

//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

view.setVisibility(View.VISIBLE);

//获取到状态栏的高度

            int statusHeight =getStatusBarHeight(activity);

try {

if (view.getParent()instanceof LinearLayout) {

//动态的设置隐藏布局的高度

                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();

params.height = statusHeight;

view.setLayoutParams(params);

}else if (view.getParent()instanceof RelativeLayout) {

//动态的设置隐藏布局的高度

                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();

params.height = statusHeight;

view.setLayoutParams(params);

}else if (view.getParent()instanceof FrameLayout) {

//动态的设置隐藏布局的高度

                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();

params.height = statusHeight;

view.setLayoutParams(params);

}

}catch (Exception e) {

e.printStackTrace();

Toast.makeText(activity,"调用initState()时布局类型转换错误", Toast.LENGTH_LONG).show();

}

}

}

/**

    * @param activity

    * @param useThemeStatusBarColor 是否要设置状态栏的颜色,不设置则为透明色

*/

    public static void setStatusBar(Activity activity,boolean useThemeStatusBarColor,int color) {

if (checkDeviceHasNavigationBar(activity)) {

//            activity.getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, StateBarUtil.getNavigationBarHeight(activity));

            activity.getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0,0,0,0);

}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上

            View decorView = activity.getWindow().getDecorView();

int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;

decorView.setSystemUiVisibility(option);

//根据上面设置是否对状态栏单独设置颜色

            if (useThemeStatusBarColor) {

activity.getWindow().setStatusBarColor(color);

}else {

//部分机型的stateBar会有半透明的黑色背景

                activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS/* | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION*/);

activity.getWindow().setStatusBarColor(Color.TRANSPARENT);

}

}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0  状态栏的颜色以最外层布局的背景色为准。

            WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();

localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

}

}

/**

* 改变魅族的状态栏字体为黑色,要求FlyMe4以上

*/

    private static void processFlyMe(boolean isLightStatusBar, Activity activity) {

WindowManager.LayoutParams lp = activity.getWindow().getAttributes();

try {

Class instance = Class.forName("android.view.WindowManager$LayoutParams");

int value = instance.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON").getInt(lp);

Field field = instance.getDeclaredField("meizuFlags");

field.setAccessible(true);

int origin = field.getInt(lp);

if (isLightStatusBar) {

field.set(lp, origin | value);

}else {

field.set(lp, (~value) & origin);

}

}catch (Exception ignored) {

ignored.printStackTrace();

}

}

/**

* 改变小米的状态栏字体颜色为黑色, 要求MiUi6以上  lightStatusBar为真时表示黑色字体

*/

    private static void processMIUI(boolean lightStatusBar, Activity activity) {

Class clazz = activity.getWindow().getClass();

try {

int darkModeFlag;

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(), lightStatusBar ? darkModeFlag :0, darkModeFlag);

//开发版 7.7.13 及以后版本采用了系统API,旧方法无效但不会报错,所以两个方式都要加上

            if (lightStatusBar) {

setStatusDarkTextColor(activity);

}else {

setStatusTintTextColor(activity);

}

}catch (Exception ignored) {

ignored.printStackTrace();

}

}

private static final StringKEY_MI_VERSION_CODE ="ro.miui.ui.version.code";

private static final StringKEY_MI_VERSION_NAME ="ro.miui.ui.version.name";

private static final StringKEY_MI_INTERNAL_STORAGE ="ro.miui.internal.storage";

/**

* 判断手机是否是小米

*

    * @return

    */

    private static boolean isMiUi() {

try {

final BuildProperties prop = BuildProperties.newInstance();

return prop.getProperty(KEY_MI_VERSION_CODE,null) !=null

                    || prop.getProperty(KEY_MI_VERSION_NAME,null) !=null

                    || prop.getProperty(KEY_MI_INTERNAL_STORAGE,null) !=null;

}catch (final IOException e) {

return false;

}

}

/**

* 判断手机是否是魅族

*

    * @return

    */

    private static boolean isFly() {

try {

final Method method = Build.class.getMethod("hasSmartBar");

return method !=null;

}catch (final Exception e) {

return false;

}

}

/**

* 深色

*

    * @param activity

    */

    private static void setStatusDarkTextColor(Activity activity) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0

            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

}

}

/**

* 浅色

*

    * @param activity

    */

    private static void setStatusTintTextColor(Activity activity) {

activity.getWindow().getDecorView().setSystemUiVisibility(

View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

}

/**

* 设置状态栏文字色值为深色调

*

    * @param useDart  是否使用深色调

    * @param activity

    */

    public static void setStatusTextColor(boolean useDart, Activity activity) {

if (isFly()) {

processFlyMe(useDart, activity);

}else if (isMiUi()) {

processMIUI(useDart, activity);

}else {

if (useDart) {

setStatusDarkTextColor(activity);

}else {

setStatusTintTextColor(activity);

}

}

}

/**

* 通过反射的方式获取状态栏高度

*

    * @return

    */

    public static int getStatusBarHeight(Context context) {

try {

Class c = Class.forName("com.android.internal.R$dimen");

Object obj = c.newInstance();

Field field = c.getField("status_bar_height");

int x = Integer.parseInt(field.get(obj).toString());

return context.getResources().getDimensionPixelSize(x);

}catch (Exception e) {

e.printStackTrace();

}

return 0;

}

/**

* 判断是否有底部导航栏

*

    * @param context

    * @return

    */

    private static boolean checkDeviceHasNavigationBar(Context context) {

boolean hasNavigationBar =false;

Resources rs = context.getResources();

int id = rs.getIdentifier("config_showNavigationBar","bool","android");

if (id >0) {

hasNavigationBar = rs.getBoolean(id);

}

try {

Class systemPropertiesClass = Class.forName("android.os.SystemProperties");

Method m = systemPropertiesClass.getMethod("get", String.class);

String navBarOverride = (String) m.invoke(systemPropertiesClass,"qemu.hw.mainkeys");

if ("1".equals(navBarOverride)) {

hasNavigationBar =false;

}else if ("0".equals(navBarOverride)) {

hasNavigationBar =true;

}

}catch (Exception e) {

e.getStackTrace();

}

return hasNavigationBar;

}

/**

* 获取底部导航栏高度

*

    * @return

    */

    private static int getNavigationBarHeight(Context context) {

Resources resources = context.getResources();

int resourceId = resources.getIdentifier("navigation_bar_height","dimen","android");

return resources.getDimensionPixelSize(resourceId);

}

private static class BuildProperties {

private final Propertiesproperties;

private BuildProperties()throws IOException {

properties =new Properties();

properties.load(new FileInputStream(new File(Environment.getRootDirectory(),"build.prop")));

}

public boolean containsKey(final Object key) {

return properties.containsKey(key);

}

public boolean containsValue(final Object value) {

return properties.containsValue(value);

}

public Set> entrySet() {

return properties.entrySet();

}

public String getProperty(final String name) {

return properties.getProperty(name);

}

private String getProperty(final String name,final String defaultValue) {

return properties.getProperty(name, defaultValue);

}

public boolean isEmpty() {

return properties.isEmpty();

}

public Enumeration keys() {

return properties.keys();

}

public Set keySet() {

return properties.keySet();

}

public int size() {

return properties.size();

}

public Collection values() {

return properties.values();

}

private static BuildProperties newInstance()throws IOException {

return new BuildProperties();

}

}

}

你可能感兴趣的:(Android沉浸式状态栏)