Android開發中的狀態欄透明而且改成應用的顏色,只是其中的一個方法

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上  //設置狀態欄透明
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
        WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
        localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
    }
    addStatusBarView();

}

//其中android.R.id.content是一個放根佈局的一個容器,最後addView能夠覆蓋狀態欄,只需設置顏色便可達到目的
//其中跟佈局還有一個屬性必須設置:
android:fitsSystemWindows="true"
private void addStatusBarView() {
    View view = new View(this);
    view.setBackgroundColor(getResources().getColor(R.color.statusBarColor));
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            getStatusBarHeight(this));
    ViewGroup decorView = (ViewGroup) findViewById(android.R.id.content);
    decorView.addView(view, params);
}

集成一下直接調用:其實須要3步:android

     a.根佈局android:fitsSystemWindows="true"佈局

     b.狀態欄透明ui

     c.本身的view覆蓋狀態欄this

public class ZzmStatusBarUtils {
    public void setStatusBarColor(AppCompatActivity activity) {
        //設置透明
        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);
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            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);
        }
        //自定義的view覆蓋狀態欄
        View view = new View(activity);
        view.setBackgroundColor(activity.getResources().getColor(R.color.statusBarColor));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                getStatusBarHeight(activity));
        ViewGroup decorView = (ViewGroup) activity.findViewById(android.R.id.content);
        decorView.addView(view, params);
    }
相關文章
相關標籤/搜索