Android透明状态栏

带遮罩

在values/styles下添加一个复制一份styles.xml为v19版本的styles.xml,里面加上

  <item name="windowNoTitle">true</item>
  <item name="android:windowTranslucentStatus">true</item>     

在布局文件中,添加

android:fitsSystemWindows="true"

这样效果如下

代码如下

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/bg"
    tools:context="com.study.zhoujun.myfirstapp.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>
</RelativeLayout>

完全透明

实现完全透明不需要在styles.xml文件中添加

 <item name="android:windowTranslucentStatus">true</item>   

只需要

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

然后main_activity.xml代码不变,java文件中添加

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      Window window = getWindow();
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      window.setStatusBarColor(Color.TRANSPARENT);
   }

效果如下

参考资料:

https://www.zhihu.com/question/36284456/answer/66781554
http://www.jianshu.com/p/aca4fd6743b1

更新

上面的透明状态栏只适用于布局文件的跟布局颜色或图片的沉浸,如果需要沉浸的是布局下的跟布局中的一个子布局的沉浸,会出现变为白边的情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值