Android 4.4 沉浸式透明状态栏

本文介绍了两种实现Android应用中透明状态栏的方法。一种是在代码中通过添加特定的窗口标志实现,但可能导致布局错位;另一种是通过修改Activity的XML布局文件或者在自定义主题中设置属性来实现。

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

原文链接:http://www.bkjia.com/Androidjc/913061.html

  • 第一种方法

这里写代码片第一种方法,在代码设置:

if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
直接调用上面2行代码可以透明,但是你会发现你的 view 跑到 actionbar 上面去了,很明显 google 的意图是使你的 view 可以占据整个屏幕,然后 状态栏和导航栏 透明覆盖在上面很明显这样不可行。
那有没有办法使你的 view 保持原来大小呢?
有,你需要在这个 activity 的 layout xml 文件添加两个属性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"

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

    android:orientation="vertical" >
  • 第二种方法

    第二种方式,是设置 theme 属性

android:theme=”@android:style/Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor”
android:theme=”@android:style/Theme.Holo.Light.NoActionBar.TranslucentDecor”
android:theme=”@android:style/Theme.Holo.NoActionBar.TranslucentDecor”
复制代码 如果你使用自定主题,只需在在 values-19 文件添加以下属性:

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">

        <!-- API 19 theme customizations can go here. -->
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

刚刚说了这个使用有局限性,不过好在有一个开源的东西
https://github.com/jgilfelt/SystemBarTint

使用这个开源库,必须开启透明标题栏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值