(解决)android不同版本的【沉浸式状态栏】(4.4/5.0/5.1/6.0)

来源:互联网 发布:贾巴尔数据 编辑:程序博客网 时间:2024/05/16 10:50

介绍:

各个版本有略微的区别,下面我就根据自己的测试和调试写出对应的方法:

4.4以上(API>=19)的前提下,任选以下方法:

  • 1.法1:只要调用一个方法,setContentView前:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  • 2.法2:写一个基类,然后对于想要沉浸的菜单栏,继承其即可。
public class ActivityBase extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);//只对api19以上版本有效        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            setTranslucentStatus(true);        }        //为状态栏着色        SystemBarTintManager tintManager = new SystemBarTintManager(this);        tintManager.setStatusBarTintEnabled(true);//状态栏颜色        tintManager.setStatusBarTintResource(Color.TRANSPARENT);    }    @TargetApi(19)    private void setTranslucentStatus(boolean on) {        Window win = getWindow();        WindowManager.LayoutParams winParams = win.getAttributes();        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;        if (on) {            winParams.flags |= bits;        } else {            winParams.flags &= ~bits;        }        win.setAttributes(winParams);    }}

(PS:其中上面的“SystemBarTintManager 类”大家百度一下即可下载)


api >21(5.0以上的)

对于api >21(5.0以上的),如果用上述的方法,也能看到透明,但是有一个暗底,所以看起来不算完全的沉浸式,所以采用以下方法即可。

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mActivitUtil = ActivitUtil.getActivitUtil();        mActivitUtil.addActivit(this);        //沉浸式状态栏在API>=19的前提下,只要调用一个方法//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        //api 21(5.0)以上需要添加此判断,可以不要上面那句        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            Window window = getWindow();            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);//            window.setStatusBarColor(Color.TRANSPARENT);//6.0的真机上反而是用这句生效            window.setStatusBarColor(getResources().getColor(R.color.title_color));        }        //还要设置偏移,否则状态栏和内容重叠        View view = View.inflate(this, R.layout.activity_main, null);        LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(                ViewGroup.LayoutParams.MATCH_PARENT,                ViewGroup.LayoutParams.MATCH_PARENT);        LinearLayout content = new LinearLayout(this);        content.setPadding(0,getStatusBarHeight(),0,0);        content.addView(ll,lp);        setContentView(content);    }    /**     * 获取状态栏高度     *     * @return     */    private int getStatusBarHeight() {        int result = 0;        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");        if (resourceId > 0) {            result = getResources().getDimensionPixelSize(resourceId);        }        return result;    }    /**     * 状态栏添加到布局中,发现不加也成功     *     *///    private void addStatusBar(ViewGroup viewGroup) {//        mStatusBar = new View(this);//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(//                ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());//        mStatusBar.setLayoutParams(lp);//        mStatusBar.setBackgroundColor(Color.TRANSPARENT);//        viewGroup.addView(mStatusBar);//    }
0 0
原创粉丝点击