Android 自定义沉浸式标题

由于项目需要,基于LinearLayout自定义的沉浸式菜单栏:

1、支持图片背景,纯色背景、透明背景

2、支持修改状态栏字体颜色

3、定义7中常用样式(白色背景字体黑色、图片背景字体白色、透明背景字体白色、透明背景字体黑色、自定义颜色、无标题状态栏字体白色、无标题状态栏字体黑色)

4、适应WebView 使用,添加了 关闭按钮、进度条


布局文件

common_navigation_status_title_layout.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/parentView_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <View
            android:id="@+id/toolBar_v"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <RelativeLayout
            android:id="@+id/title_rl"
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/left_title_ll"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <ImageView
                    android:id="@+id/left_btn_iv"
                    style="@style/Text.Title_Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:contentDescription="标题左侧图标"
                    android:scaleType="centerInside"
                    android:src="@drawable/common_back_theme" />

                <TextView
                    android:id="@+id/left_btn_tv"
                    style="@style/Text.Title_Button"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_toRightOf="@id/left_btn_iv"
                    android:text="返回"
                    android:textColor="@color/global_black_color"
                    android:textSize="@dimen/title_other_text_size" />
            </LinearLayout>

            <ImageView
                android:id="@+id/cloose_btn_iv"
                style="@style/Text.Title_Button"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/left_title_ll"
                android:adjustViewBounds="true"
                android:contentDescription="关闭图标"
                android:padding="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/common_close_white" />

            <TextView
                android:id="@+id/title_tv"
                style="@style/Text.Title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:text="标题"
                android:textColor="@color/global_black_color"
                android:textSize="@dimen/title_text_size" />

            <LinearLayout
                android:id="@+id/right_title_ll"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">

                <TextView
                    android:id="@+id/right_btn_tv"
                    style="@style/Text.Title_Button"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="更多"
                    android:textColor="@color/global_black_color"
                    android:textSize="@dimen/title_other_text_size" />

                <ImageView
                    android:id="@+id/right_btn_iv"
                    style="@style/Text.Title_Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:contentDescription="标题右侧图标"
                    android:scaleType="centerInside"
                    android:src="@drawable/common_choose_right" />
            </LinearLayout>

            <com.kdlc.mcc.ui.bar.KdlcProgressBar
                android:id="@+id/progress_bar_h"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="3dp"
                android:progressDrawable="@drawable/progressbar_horizontal" />

            <View
                android:id="@+id/bottom_line_v"
                android:layout_width="fill_parent"
                android:layout_height="1px"
                android:layout_alignParentBottom="true"
                android:background="@color/theme_line_color" />
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

自定义沉浸式导航

ToolBarTitleView.java

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kdlc.mcc.R;
import com.kdlc.mcc.ui.bar.KdlcProgressBar;
import com.kdlc.utils.statusbar.StatusBarUtil;
import com.socks.library.KLog;
import com.xybt.common.util.Tool;


public class ToolBarTitleView extends LinearLayout {
    private LinearLayout parentView_ll;//父部局
    private View toolBar_v;//状态视图
    private RelativeLayout title_rl;
    private LinearLayout left_title_ll;
    private LinearLayout right_title_ll;
    private ImageView left_btn_iv;
    private TextView left_btn_tv;
    private ImageView cloose_btn_iv;
    private TextView title_tv;
    private ImageView right_btn_iv;
    private TextView right_btn_tv;
    private View bottom_line_v;
    private KdlcProgressBar progress_bar_h;

    private StatusBarUtil toolBarUtil;


    private final int WHITE = 100;//白色
    private final int IMG = 101;//图片
    private final int TRAN_WHITE = 102;//透明 白色字体
    private final int TRAN_BLCAK = 103;//透明 黑色字体
    private final int THEME = 104;//主题色
    private final int NO_TITLE_WHITE = 105;//无标题 白色字体
    private final int NO_TITLE_BLCAK = 106;//无标题 黑色字体

    private int DEFULT_STYLE = IMG;
    private int DEFUT_IMG_BG = R.drawable.icon_title_bg;

    private Context context;

    public ToolBarTitleView(Context context) {
        this(context, null);
    }

    public ToolBarTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ToolBarTitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        toolBarUtil = new StatusBarUtil((Activity) context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.common_navigation_status_title_layout, this, true);
        initViews();
        hidenAll();
        initStyle(attrs);
        setHeight();
    }

    /**
     * 初始化视图
     */
    public void initViews() {
        parentView_ll = (LinearLayout) findViewById(R.id.parentView_ll);
        toolBar_v = (View) findViewById(R.id.toolBar_v);
        title_rl = (RelativeLayout) findViewById(R.id.title_rl);
        left_title_ll = (LinearLayout) findViewById(R.id.left_title_ll);
        right_title_ll = (LinearLayout) findViewById(R.id.right_title_ll);
        left_btn_iv = (ImageView) findViewById(R.id.left_btn_iv);
        left_btn_tv = (TextView) findViewById(R.id.left_btn_tv);
        cloose_btn_iv = (ImageView) findViewById(R.id.cloose_btn_iv);
        title_tv = (TextView) findViewById(R.id.title_tv);
        right_btn_iv = (ImageView) findViewById(R.id.right_btn_iv);
        right_btn_tv = (TextView) findViewById(R.id.right_btn_tv);
        bottom_line_v = (View) findViewById(R.id.bottom_line_v);
        progress_bar_h = (KdlcProgressBar) findViewById(R.id.progress_bar_h);

    }

    public void initStyle(AttributeSet attrs) {
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ToolBarTitleView);
        if (attributes != null) {
            //处理titleBar背景色
            int titleBarBackGround = attributes.getResourceId(R.styleable.ToolBarTitleView_title_background, -1);

            DEFULT_STYLE = attributes.getInt(R.styleable.ToolBarTitleView_defult_style, DEFULT_STYLE);

            switch (DEFULT_STYLE) {
                case WHITE:
                    initWhiteBg();
                    break;
                case IMG:
                    if (titleBarBackGround == -1) {
                        initImgBg(DEFUT_IMG_BG);
                    } else {
                        initImgBg(titleBarBackGround);
                    }
                    break;
                case TRAN_WHITE:
                    initTransparentWhiteTextBg();
                    break;
                case TRAN_BLCAK:
                    initTransparentBlackTextBg();
                    break;
                case NO_TITLE_WHITE:
                    initNoTitleWhiteTextBg();
                    break;
                case NO_TITLE_BLCAK:
                    initNoTitleBlackTextBg();
                    break;
                case THEME:
                default:
                    initThemeBg();
                    break;
            }

            if (DEFULT_STYLE != IMG && titleBarBackGround != -1)
                setToolBarTitleViewBg(titleBarBackGround);

            //标题
            String title_text = attributes.getString(R.styleable.ToolBarTitleView_title_text);
            if (!TextUtils.isEmpty(title_text))
                setTitle(title_text);

            //设置左边按钮的文字
            String leftButtonText = attributes.getString(R.styleable.ToolBarTitleView_left_button_text);
            if (!TextUtils.isEmpty(leftButtonText)) {
                setLeftButtonText(leftButtonText);
                //设置左边按钮文字颜色
                int leftButtonTextColor = attributes.getColor(R.styleable.ToolBarTitleView_left_button_text_color, -1);
                if (leftButtonTextColor != -1)
                    left_btn_tv.setTextColor(leftButtonTextColor);
            }
            //设置左边图片icon
            int leftButtonDrawable = attributes.getResourceId(R.styleable.ToolBarTitleView_left_button_drawable, -1);
            if (leftButtonDrawable != -1) {
                setLeftButtonImg(leftButtonDrawable);
            }

            //先处理右边按钮
            //设置右边按钮的文字
            String rightButtonText = attributes.getString(R.styleable.ToolBarTitleView_right_button_text);
            if (!TextUtils.isEmpty(rightButtonText)) {
                setRightButtonText(rightButtonText);
                //设置右边按钮文字颜色
                int rightButtonTextColor = attributes.getColor(R.styleable.ToolBarTitleView_right_button_text_color, -1);
                if (rightButtonTextColor != -1)
                    right_btn_tv.setTextColor(rightButtonTextColor);
            }
            //设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
            int rightButtonDrawable = attributes.getResourceId(R.styleable.ToolBarTitleView_right_button_drawable, -1);
            if (rightButtonDrawable != -1) {
                setRightButtonImg(rightButtonDrawable);
            }

            //处理关闭按钮
            boolean close_iv_visible = attributes.getBoolean(R.styleable.ToolBarTitleView_close_iv_visible, false);
            if (close_iv_visible)
                showCloseImg();

            //显示底部line
            boolean bottom_line_visible = attributes.getBoolean(R.styleable.ToolBarTitleView_bottom_line_visible, false);
            if (bottom_line_visible)
                showBottomLine();
            attributes.recycle();
        }
    }

    /**
     * 隐藏所有视图
     */

    public void hidenAll() {
        left_btn_iv.setVisibility(View.GONE);
        left_btn_tv.setVisibility(View.GONE);
        cloose_btn_iv.setVisibility(View.GONE);
        title_tv.setVisibility(View.GONE);
        right_btn_tv.setVisibility(View.GONE);
        right_btn_iv.setVisibility(View.GONE);
        bottom_line_v.setVisibility(View.INVISIBLE);
        progress_bar_h.setVisibility(View.GONE);
    }

    /**
     * 设置 透明背景 白色字体
     */
    public void initTransparentWhiteTextBg() {
        setToolBarTitleViewBg(R.color.transparent);
        setTextColor(R.color.global_white_color);
        toolBarUtil.setTranslucentStatus(true);
        toolBarUtil.setStatusTextColor(false);
    }

    /**
     * 设置 透明背景 黑色字体
     */
    public void initTransparentBlackTextBg() {
        setToolBarTitleViewBg(R.color.transparent);
        setTextColor(R.color.global_black_color);
        toolBarUtil.setTranslucentStatus(true);
        toolBarUtil.setStatusTextColor(true);
    }

    /**
     * 设置 透明背景 黑色字体
     */
    public void initNoTitleBlackTextBg() {
        title_rl.setVisibility(GONE);
        setToolBarTitleViewBg(R.color.transparent);
        toolBarUtil.setTranslucentStatus(true);
        toolBarUtil.setStatusTextColor(true);
    }

    /**
     * 设置 透明背景 白色字体
     */
    public void initNoTitleWhiteTextBg() {
        title_rl.setVisibility(GONE);
        setToolBarTitleViewBg(R.color.transparent);
        toolBarUtil.setTranslucentStatus(true);
        toolBarUtil.setStatusTextColor(false);
    }

    /**
     * 设置 主题色背景
     */
    public void initThemeBg() {
        setToolBarTitleViewBg(R.color.theme_color);
        setTextColor(R.color.global_white_color);
        toolBarUtil.setTranslucentStatus(true);
        toolBarUtil.setStatusTextColor(false);
    }

    /**
     * 设置 白色背景
     */
    public void initWhiteBg() {
        setToolBarTitleViewBg(R.color.global_white_color);
        setTextColor(R.color.theme_color);
        toolBarUtil.setTranslucentStatus(true);
        toolBarUtil.setStatusTextColor(true);
    }

    /**
     * 设置 图片背景
     */
    public void initImgBg(int imgId) {
        setToolBarTitleViewBg(imgId);
        setTextColor(R.color.global_white_color);
        toolBarUtil.setTranslucentStatus(true);
        toolBarUtil.setStatusTextColor(false);
    }

    /**
     * 设置状态栏高度
     */
    public void setHeight() {
        title_rl.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                KLog.d("ToolBar", "Title Height " + title_rl.getHeight() + " Parent Height " + parentView_ll.getHeight() + " ToolBar Height " + toolBar_v.getHeight() + " SYSBAR Height" + getStatusBarHeight());
                title_rl.getViewTreeObserver().removeOnPreDrawListener(this);
                LinearLayout.LayoutParams p_params = (LinearLayout.LayoutParams) parentView_ll.getLayoutParams();//获取当前控件的布局对象
                p_params.height = getStatusBarHeight() + title_rl.getHeight();//设置当前控件布局的高度
                parentView_ll.setLayoutParams(p_params);
                LinearLayout.LayoutParams tool_params = (LinearLayout.LayoutParams) toolBar_v.getLayoutParams();//获取当前控件的布局对象
                tool_params.height = getStatusBarHeight();//设置当前控件布局的高度
                toolBar_v.setLayoutParams(tool_params);
                KLog.d("ToolBar", "Title Height " + title_rl.getHeight() + " Parent Height " + parentView_ll.getHeight() + " ToolBar Height " + toolBar_v.getHeight() + " SYSBAR Height" + getStatusBarHeight());
                return true;
            }
        });
    }

    /**
     * 返回 状态栏+导航栏高度
     *
     * @return
     */
    public int getToolBarTitleViewHeight() {
        return parentView_ll.getHeight();
    }

    /**
     * 设置 背景色
     *
     * @param bgid
     */
    public void setToolBarTitleViewBg(int bgid) {
        if (parentView_ll != null) {
            parentView_ll.setBackgroundDrawable(getContext().getResources().getDrawable(bgid));
        }
    }

    public void setBottomViewPaddingTop(View view, boolean isPadding) {
        if (isPadding) {
            view.setPadding(0, getToolBarTitleViewHeight(), 0, 0);
        } else {
            view.setPadding(0, 0, 0, 0);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    /**
     * 获取状态栏 高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return 0;
        } else {
            return Tool.getStatusBarHeight(context);
        }
    }

    /**
     * 设置字体颜色
     *
     * @param colorRes
     */
    public void setTextColor(@ColorRes int colorRes) {
        int color = getResources().getColor(colorRes);
        left_btn_tv.setTextColor(color);
        title_tv.setTextColor(color);
        right_btn_tv.setTextColor(color);
    }

    public void setTitle(String text) {
        title_tv.setVisibility(View.VISIBLE);
        title_tv.setText(text);
    }

    public void setTitle(int textid) {
        title_tv.setVisibility(View.VISIBLE);
        title_tv.setText(textid);
    }

    public void setLeftButtonText(String text) {
        left_btn_tv.setVisibility(View.VISIBLE);
        left_btn_tv.setText(text);
    }

    public void setLeftButtonText(int textid) {
        left_btn_tv.setVisibility(View.VISIBLE);
        left_btn_tv.setText(textid);
    }

    public void setLeftButtonImg(int imgId) {
        left_btn_iv.setVisibility(View.VISIBLE);
        left_btn_iv.setImageResource(imgId);
    }

    public void setLeftClickListener(OnClickListener listener) {
        left_title_ll.setOnClickListener(listener);
    }

    public void setRightButtonText(String text) {
        right_btn_tv.setVisibility(View.VISIBLE);
        right_btn_tv.setText(text);
    }

    public void setRightButtonText(int textid) {
        right_btn_tv.setVisibility(View.VISIBLE);
        right_btn_tv.setText(textid);
    }

    public void setRightButtonImg(int imgId) {
        right_btn_iv.setVisibility(View.VISIBLE);
        right_btn_iv.setImageResource(imgId);
    }

    public void setRightClickListener(OnClickListener listener) {
        right_title_ll.setOnClickListener(listener);
    }

    /**
     * 隐藏底部线
     */
    public void hideBottomLine() {
        bottom_line_v.setVisibility(View.INVISIBLE);
    }

    /**
     * 显示底部线
     */
    public void showBottomLine() {
        bottom_line_v.setVisibility(View.VISIBLE);
    }


    public void setCloseImgListener(OnClickListener listener) {
        cloose_btn_iv.setOnClickListener(listener);
    }

    public void showCloseImg() {
        cloose_btn_iv.setVisibility(View.VISIBLE);
    }

    public void hideCloseImg() {
        cloose_btn_iv.setVisibility(View.GONE);
    }

    public void showProgress() {
        progress_bar_h.setVisibility(View.VISIBLE);
    }

    public void hideProgress() {
        progress_bar_h.setVisibility(View.GONE);
    }

    public void setAnimProgress(int progress) {
        progress_bar_h.setVisibility(View.VISIBLE);
        progress_bar_h.setAnimProgress2(progress);
    }

    public LinearLayout getParentView_ll() {
        return parentView_ll;
    }

    public View getToolBar_v() {
        return toolBar_v;
    }

    public RelativeLayout getTitle_rl() {
        return title_rl;
    }

    public LinearLayout getLeft_title_ll() {
        return left_title_ll;
    }

    public LinearLayout getRight_title_ll() {
        return right_title_ll;
    }

    public ImageView getLeft_btn_iv() {
        return left_btn_iv;
    }

    public TextView getLeft_btn_tv() {
        return left_btn_tv;
    }

    public ImageView getCloose_btn_iv() {
        return cloose_btn_iv;
    }

    public TextView getTitle_tv() {
        return title_tv;
    }

    public ImageView getRight_btn_iv() {
        return right_btn_iv;
    }

    public TextView getRight_btn_tv() {
        return right_btn_tv;
    }

    public View getBottom_line_v() {
        return bottom_line_v;
    }

    public KdlcProgressBar getProgress_bar_h() {
        return progress_bar_h;
    }

}

控制状态栏字体颜色

StatusBarUtil.java

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.kdlc.utils.BuildProperties;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 状态栏工具类
 */
public class StatusBarUtil {

    private Activity activity;

    public StatusBarUtil(Activity activity) {
        this.activity = activity;
    }

    /**
     * @param ShowSysStatusBar 是否不显示系统的状态栏
     */
    @TargetApi(19)
    public void setTranslucentStatus(boolean ShowSysStatusBar) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (ShowSysStatusBar) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
        //再次创建,有时候上面没有初始化状态栏
        SystemBarTintManager tintManager = new SystemBarTintManager(activity);
        tintManager.setStatusBarTintEnabled(!ShowSysStatusBar);
    }


    /**
     * 设置状态栏文字色值为深色调
     *
     * @param useDart 是否使用深色调
     */
    public void setStatusTextColor(boolean useDart) {
        if (isFlyme()) {
            processFlyMe(useDart);
        } else if (isMIUI()) {
            processMIUI(useDart);
        } else {
            processAndroid(useDart);
        }
    }


    //**************** Flyme ****************

    /**
     * 判断手机是否是魅族
     *
     * @return
     */
    public boolean isFlyme() {
        try {
            // Invoke Build.hasSmartBar()
            final Method method = Build.class.getMethod("hasSmartBar");
            return method != null;
        } catch (final Exception e) {
            return false;
        }
    }

    /**
     * 改变魅族的状态栏字体为黑色,要求FlyMe4以上
     */
    private void processFlyMe(boolean useDark) {
        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 (useDark) {
                field.set(lp, origin | value);
            } else {
                field.set(lp, (~value) & origin);
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }


    //**************** MIUI ****************
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

    /**
     * 判断手机是否是小米
     *
     * @return
     */
    public boolean isMIUI() {
        try {
            final BuildProperties prop = BuildProperties.newInstance();
            return prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
                    || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
                    || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;
        } catch (final IOException e) {
            return false;
        }
    }

    /**
     * 改变小米的状态栏字体颜色为黑色, 要求MIUI6以上  lightStatusBar为真时表示黑色字体
     * <p>
     * http://www.miui.com/thread-8946673-1-1.html
     * <p>
     * 附:开发者应该做的修改
     * 如果开发者需要设置「状态栏黑色字符」的效果, 需要做以下几件事:
     * <p>
     * 1. 在新的 MIUI 版本(即基于 Android 6.0 ,开发版 7.7.13 及以后版本):
     * - 使用 `View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR` ,来设置「状态栏黑色字符」效果
     * - 同时要设置`WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS`,
     * - 并且无 `WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS`
     * <p>
     * 参考实例:
     * Window window = getWindow();
     * window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
     * window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
     * window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
     * <p>
     * 2. 在旧的MIUI版本
     * 还有大量的米粉使用旧的MIUI版本,因此仍然需要使用 MIUI 原有的方法,即
     * Window window = getWindow();
     * window.addExtraFlags(MiuiWindowManager.LayoutParams.EXTRA_FLAG_STATUS_BAR_DARK_MODE);
     */
    private void processMIUI(boolean useDart) {
        Window window = activity.getWindow();
        if (window == null) {
            return;
        }

        Class<? extends Window> clazz = window.getClass();
        try {
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            int darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(window, useDart ? darkModeFlag : 0, darkModeFlag);
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }

        if (useDart) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    }


    //**************** Android ****************
    private void processAndroid(boolean useDart) {
        Window window = activity.getWindow();
        if (window == null) {
            return;
        }

        if (useDart) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
        window.getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, 0);
    }
}

build.prop信息获取

BuildProperties.java

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by SmileXie on 2017/7/4.
 */

public class BuildProperties {

    private final Properties properties;

    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<Map.Entry<Object, Object>> entrySet() {
        return properties.entrySet();
    }

    public String getProperty(final String name) {
        return properties.getProperty(name);
    }

    public String getProperty(final String name, final String defaultValue) {
        return properties.getProperty(name, defaultValue);
    }

    public boolean isEmpty() {
        return properties.isEmpty();
    }

    public Enumeration<Object> keys() {
        return properties.keys();
    }

    public Set<Object> keySet() {
        return properties.keySet();
    }

    public int size() {
        return properties.size();
    }

    public Collection<Object> values() {
        return properties.values();
    }

    public static BuildProperties newInstance() throws IOException {
        return new BuildProperties();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值