Android沉浸式状态栏

来源:互联网 发布:管家婆软件的销售成本 编辑:程序博客网 时间:2024/06/11 07:38

Android版本4.4(API19)之后,就可以对状态栏进行设置了,在浏览了许多资料和自己的实践之后,总结了一个最简单的写法,先上效果图


废话不多说,直接上代码

我们现在在实际项目中,一般不使用actionbar,而换成toolbar了,所以我们将AppTheme的style添加两行代码:

 <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>

实现这种沉浸式状态栏有两种方法

第一种,在onCreate方法中写:

//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            //透明状态栏            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            //透明导航栏            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}
第二种:

@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);}
然后在onCreate中使用该方法

//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {setTranslucentStatus(true);

现在的效果是:


可以看出,我们的布局整体往上移了,这是因为状态栏设置为透明之后,整体布局已经延伸到了状态栏,既然这样,那我们只需要用一个控件对状态栏进行占位就行

java代码:

private int getStatusBarHeight(){int statusBarHeight1=0;int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {//根据资源ID获取响应的尺寸值statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);}return statusBarHeight1;}

xml代码:

 <LinearLayout        android:layout_width="match_parent"        android:layout_height="1dp"        android:orientation="vertical"        android:visibility="gone"        android:fitsSystemWindows="true"        android:background="#3f51b5"        android:id="@+id/linea"></LinearLayout>



最后在onCreate中使用

LinearLayout linear_bar= (LinearLayout) findViewById(R.id.linea);linear_bar.setVisibility(View.VISIBLE);int statusHeight=getStatusBarHeight();LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) linear_bar.getLayoutParams();params.height=statusHeight;linear_bar.setLayoutParams(params);

来看看效果


各位应该看到占位控件中写了一个background,这里设置的颜色就是我们状态栏的颜色

当然了,实际项目中一般用的都是toolbar,那么如果用到了toolbar,我们就不需要再多写占位控件了,只需要在根布局上添加

android:fitsSystemWindows="true"

同样的,也需要在根布局中添加一个背景颜色来给状态栏着色

fitsSystemWindows的官方描述

Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

大致意思就是:

这个一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为true,就会调整view的paingding属性来给system windows留出空间

实际效果:

当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。

5.0(API21)版本以上的效果:


4.4-5.0(API19-API21)版本的效果


当然还有利用第三方库的:

compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

GitHub地址

使用该库的话就不需要再用占位控件了,直接上代码:

//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//透明状态栏getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明导航栏getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);SystemBarTintManager tintManager = new SystemBarTintManager(this);// 激活状态栏tintManager.setStatusBarTintEnabled(true);// enable navigation bar tint 激活导航栏tintManager.setNavigationBarTintEnabled(true);//设置系统栏设置颜色//tintManager.setTintColor(R.color.red);//给状态栏设置颜色tintManager.setStatusBarTintResource(R.color.middle_red);// 设置导航栏设置资源tintManager.setNavigationBarTintResource(R.color.color_nav);}

然后在xml中的根布局添加

android:clipToPadding="true"    android:fitsSystemWindows="true"

效果是相同的,当然这个库的功能还有很多,具体的就自己去探索吧

                                             
0 0
原创粉丝点击