1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android沉浸式状态栏白色 Android沉浸式状态栏

android沉浸式状态栏白色 Android沉浸式状态栏

时间:2020-01-29 00:00:09

相关推荐

android沉浸式状态栏白色 Android沉浸式状态栏

手把手教你实现

首选: android在4.4版本上添加了WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS 和 WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,即透明的状态栏和导航栏,在使用过程中,一般会配合fitsSystemWindows和clipToPadding属性一起使用

android:fitsSystemWindows="true"

android:clipToPadding="true"

当然,如果你使用了Toolbar,可以通过以下方式设置

1.在App主题中添加 windowTranslucentStatus 和 windowIsTranslucent 属性

false

true

true

true

@android:color/transparent

也可以在Activity中通过代码设置实现上述功能,因为在部分机型上,xml设置属性无效

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();

localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

}

2.在Toolbar添加相关属性,使得Toolbar不受状态栏影响变形

android:id="@+id/appbar_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:clipToPadding="true"

android:fitsSystemWindows="true"

android:theme="@style/AppTheme.AppBarOverlay"

toolbar:elevation="0.5dp">

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="?attr/colorPrimary"

android:clipToPadding="true"

android:fitsSystemWindows="true"

android:minHeight="?attr/actionBarSize"

toolbar:popupTheme="@style/AppTheme.PopupOverlay" />

使用android:fitsSystemWindows="true",就可以调整内容布局(估计也是在根布局上加padding)恢复到原来位置

在开发过程中,有事我们还会使用到Dawerlayout,针对Drawerlayout我们也需要实现沉浸式,所以如果开发中使用了Drawerlayout,需要设置以下代码实现Drawerlayout沉浸式

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();

local LayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);

if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){

//将侧边栏顶部延伸至status bar

mDrawerLayout.setFitsSystemWindows(true);

//将主页面顶部延伸至status bar;虽默认为false,但经测试,DrawerLayout需显示设置

mDrawerLayout.setClipToPadding(false);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。