当前位置 : 主页 > 编程语言 > java >

Android 沉浸式状态栏

来源:互联网 收集:自由互联 发布时间:2021-06-28
废话不多说,直接上代码 package com.iutillib;import android.app.Activity;import android.app.Dialog;import android.os.Build;import android.view.Window;import android.view.WindowManager;import java.lang.reflect.Field;/** * Created b
废话不多说,直接上代码
package com.iutillib;

import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2017/1/19.
 */

public class StatusBarUtils {
    public static void setWindowStatusBarColor(Activity activity, int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(colorResId));

                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过反射的方式获取状态栏高度 * * @return
     */
    public static int getStatusBarHeight(Activity activity) {
        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 activity.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }



    public static void setWindowStatusBarColor(Dialog dialog, int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = dialog.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(dialog.getContext().getResources().getColor(colorResId));

                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
注意要修改style
当背景为图片时,布局可以这么写:
 
 
  
 
参考:
http://blog.csdn.net/fan7983377/article/details/51604657
上一篇:RedisUtil.java
下一篇:字符串压缩解压
网友评论