今天要记录的是Android沉浸式状态栏的开发,看到别的应用都有沉浸式状态栏,是不是感觉很爽,今天我也来做一个沉浸式状态栏的例子,下面看一下效果图:
下面记录一下实现过程:
1、首先在需要使用沉浸式状态栏的Activity里,加上如下代码:
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//判断当前SDK版本号,如果是4.4以上,就是支持沉浸式状态栏的
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
2.修改Activity对应的布局文件,加上两个属性,如下代码所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:background="#6699ff"
android:orientation="vertical">
就这俩个代码就能实现沉浸式状态栏,稍后我会把代码链接弄上来。
代码下载