您的位置:首页 > 移动开发 > Android开发

Android透明状态栏

2016-10-07 17:33 190 查看

带遮罩

在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

更新

上面的透明状态栏只适用于布局文件的跟布局颜色或图片的沉浸,如果需要沉浸的是布局下的跟布局中的一个子布局的沉浸,会出现变为白边的情况
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息