Android沉浸式状态栏(非常简单)

本文介绍了如何在Android应用中快速实现沉浸式状态栏,通过在onCreate()方法中添加特定代码,并调整styles配置,避免ToolBar与状态栏重叠问题,确保状态栏与ToolBar完美融合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

废话不多说直接上代码

在onCreate() 方法里面

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

    int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

    int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

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

        Window window = getWindow();

        WindowManager.LayoutParams attributes = window.getAttributes();

        attributes.flags |= flagTranslucentNavigation;

        window.setAttributes(attributes);

        getWindow().setStatusBarColor(Color.TRANSPARENT);

    } else {

        Window window = getWindow();

        WindowManager.LayoutParams attributes = window.getAttributes();

        attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;

        window.setAttributes(attributes);

    }
}

 这样沉浸式状态栏就搞定了

当然不要忘记styles里面改成NO

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

 这样状态栏虽然OK了但是你会发现toobar与状态栏重叠了,这时咱们就需要在布局中添加:

android:fitsSystemWindows="true"

这样就完成沉浸式状态栏 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值