关于如何实现Android透明状态栏的总结
Android 实现 如何 总结 关于 透明 状态栏
2023-09-27 14:21:17 时间
开门见山。
原来做的效果,如下图(顶部有一条明显的橙色状态栏):
![](http://upload-images.jianshu.io/upload_images/2480448-4be3fbca6ed31e9a.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/360/format/webp)
a1.gif
改过之后(顶部状态栏是透明的):
![](http://upload-images.jianshu.io/upload_images/2480448-f8278b6d6c39cb0d.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/360/format/webp)
p2.gif
我发现网上写的一些文章,不够简洁明了,我整理了一下,复制粘贴一下就可以在项目中运用。
首先,在你的Activity中添加下面四个方法(或者封装在一个工具类中)
/**
* 全透状态栏
*/
protected void setStatusBarFullTransparent() {
if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
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);
} else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//虚拟键盘也透明
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 半透明状态栏
*/
protected void setHalfTransparent() {
if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//虚拟键盘也透明
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 如果需要内容紧贴着StatusBar
* 应该在对应的xml布局文件中,设置根布局fitsSystemWindows=true。
*/
private View contentViewGroup;
protected void setFitSystemWindow(boolean fitSystemWindow) {
if (contentViewGroup == null) {
contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
}
contentViewGroup.setFitsSystemWindows(fitSystemWindow);
}
/**
* 为了兼容4.4的抽屉布局->透明状态栏
*/
protected void setDrawerLayoutFitSystemWindow() {
if (Build.VERSION.SDK_INT == 19) {//19表示4.4
int statusBarHeight = getStatusHeight(this);
if (contentViewGroup == null) {
contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
}
if (contentViewGroup instanceof DrawerLayout) {
DrawerLayout drawerLayout = (DrawerLayout) contentViewGroup;
drawerLayout.setClipToPadding(true);
drawerLayout.setFitsSystemWindows(false);
for (int i = 0; i < drawerLayout.getChildCount(); i++) {
View child = drawerLayout.getChildAt(i);
child.setFitsSystemWindows(false);
child.setPadding(0,statusBarHeight, 0, 0);
}
}
}
}
然后,在Activity的onCreate()方法中调用即可。示例如下
Activity
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
setHalfTransparent();
setFitSystemWindow(false);
}
protected void setHalfTransparent()...
protected void setStatusBarFullTransparent()...
protected void setFitSystemWindow()...
protected void setDrawerLayoutFitSystemWindow()...
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/drawerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_start">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="50dp"
android:background="#F86254"
android:text="button"
android:textColor="@color/white" />
</LinearLayout>
1.未做任何设置
可见,Android5.0以上由于默认是Material Design,顶部是蓝色状态栏。而5.0以下,默认都是黑色,而且无法修改。
![](http://upload-images.jianshu.io/upload_images/2480448-0af44bb76b43e061.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
nothing.png
2.半透明状态栏,fitsSystemWindows=false
@Override
public void init(Bundle savedInstanceState) {
setHalfTransparent();
setFitSystemWindow(false);
}
![](http://upload-images.jianshu.io/upload_images/2480448-dcd37eb68dfe82b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
half_not_fit.png
可见,5.0以上蓝色状态栏没了,变成了半透明的黑色,而内容区域则有了全屏的效果。
但是也要知道一点,那个红色的TextView,原来是紧贴着状态栏,现在是紧贴着屏幕的上边缘,这样就导致,内容被遮挡。解决这个问题需要一个关键的属性是setFitSystemWindow=true,追踪源码可知,它可以让我们的布局,paddingTop等于状态栏的高度,这样红色TextView的位置就会向下移,从而不会被遮挡。
3.半透明状态栏,fitsSystemWindows=true
@Override
public void init(Bundle savedInstanceState) {
setHalfTransparent();
setFitSystemWindow(true);
}
![](http://upload-images.jianshu.io/upload_images/2480448-b0f7aaf15a80fed9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
half_fit.png
此时红色的TextView,位于状态栏下方。
4.全透明状态栏,fitsSystemWindows=false
setStatusBarFullTransparent();
setFitSystemWindow(false);
![](http://upload-images.jianshu.io/upload_images/2480448-6cf9076e6ff6d9a3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
full_no_fit.png
全透明和半透明的区别在于,状态栏是否具有淡黑色的背景,根据项目需求合理运用。
5.全透明状态栏,fitsSystemWindows=true
setStatusBarFullTransparent();
setFitSystemWindow(true);
![](http://upload-images.jianshu.io/upload_images/2480448-2ef3fdb96aa6bf5a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
full_fit.png
6.DrawerLayout如何使用
直接使用上述方式,在4.4系统上会出现异常,因此我们需要进行适配。
修改xml文件,DrawerLayout需要添加fitsSystemWindows和clipToPadding属性,DrawerLayout布局里的一级布局,都需设置fitsSystemWindows=true。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:id="@+id/drawerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:clipToPadding="false"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="@drawable/bg_start"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#F86254"
android:text="show"
android:textColor="@color/white" />
</RelativeLayout>
<FrameLayout
android:id="@+id/sideLayout"
android:layout_width="300dp"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@drawable/bg_test">
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:background="#F86254"
android:text="button"
android:textColor="@color/white" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
- 全透明状态栏,fitsSystemWindows=false
setStatusBarFullTransparent();
![](http://upload-images.jianshu.io/upload_images/2480448-db4fb393aaad3631.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
drawerlayout_nofit.png
- DrawerLayout全透明状态栏,fitsSystemWindows=true
setStatusBarFullTransparent();
setDrawerLayoutFitSystemWindow();
![](http://upload-images.jianshu.io/upload_images/2480448-b9e5bcdeb2edb3ef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
drawerlayout.png
7.可能会错误的地方
本来我们有一个界面:
![](http://upload-images.jianshu.io/upload_images/2480448-62dc7d0cf0ca2126.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/712/format/webp)
然后按照上面的,添加了代码之后
setStatusBarFullTransparent();
setFitSystemWindow(true);
![](http://upload-images.jianshu.io/upload_images/2480448-33b38fca63b9018f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/794/format/webp)
image.png
然后你提刀来问楼主,这是什么鬼!!!
说好的透明状态栏呢,怎么状态栏背景色是白色的!
![](http://upload-images.jianshu.io/upload_images/2480448-6bc6acf4fc218750.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/592/format/webp)
确实是全屏了,状态栏也透明了,只是由于,根布局没设置背景色,默认的背景色白色,所以你看到的灰色状态栏底色,其实是根布局的TopPadding。
![](http://upload-images.jianshu.io/upload_images/2480448-b94b54406af72060.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
8.Activity中嵌套了Fragment,如何使用
另附一张效果图:
![](http://upload-images.jianshu.io/upload_images/2480448-112ecaca5cc2cc21.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/778/format/webp)
在Activity中设置setStatusBarFullTransparent(),然后在fragment的xml文件中(这边写的粗糙,应该在代码中,获取StatusBar高度然后设置paddingTop):
![](http://upload-images.jianshu.io/upload_images/2480448-300f9ddc587bee16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
有兴趣可以可以琢磨一下,为什么这么写,正所谓:学而不思则罔,思而不学则殆。
9.问题
这个方案总体来说其实不是很好,会导致过度绘制,如果对性能要求不是很严苛,可以选择该方案。
作者:桂林的小河
链接:https://www.jianshu.com/p/e89ee0a77bb5
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
相关文章
- Android so注入( inject)和Hook(挂钩)的实现思路讨论
- 使用Android Studio 1.3 版本号进行NDK开发
- ASP.NET MVC Filters 4种默认过滤器的使用【附示例】 数据库常见死锁原因及处理 .NET源码中的链表 多线程下C#如何保证线程安全? .net实现支付宝在线支付 彻头彻尾理解单例模式与多线程 App.Config详解及读写操作 判断客户端是iOS还是Android,判断是不是在微信浏览器打开
- Mic Android 8.1 方案
- Android | APP动态申请权限
- Android | setImageResource、setImageDrawable和setImageBitmap的区别
- 如何在Nexus上实现Ubuntu和Android 4.4.2 双启动
- Android实现推送方式解决方案
- 《深入解析Android 虚拟机》——导读
- Android当下最流行的开源框架总结
- 超级简单的Android Studio jni 实现(无需命令行)
- Android用悬浮按钮实现翻页效果
- Android bitmap图片处理
- Android中如何实现多行、水平滚动的分页的Gridview?
- Android视图SurfaceView的实现原理分析
- Android帧缓冲区(Frame Buffer)硬件抽象层(HAL)模块Gralloc的实现原理分析
- Android ActionBar详解(三):ActionBar实现切换Tabs标签
- android圆角View实现及不同版本这间的兼容
- Android中滑屏实现----手把手教你如何实现触摸滑屏以及Scroller类详解
- Android实现多页左右滑动效果,支持子view动态创建和cache
- Android之SurfaceView学习
- android开发,assets下面的资源文件不会变化/改动
- Android平台上使用MQTT协议实现消息推送功能
- Android-Universal-Image-Loader学习笔记(一)
- 《OpenGL ES应用开发实践指南:Android卷》—— 1.2 创建第一个程序
- Android实现戴口罩人脸检测和戴口罩识别(附Android源码)
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]
- Flutter设置App的应用名字和应用logo图标的方法(android ios web)
- 理解android中ListFragment和Loader
- Android学习之线程如何多次实现start
- Android 实现ListView不可滚动效果
- Lua学习 2) —— Android与Lua互调
- 基于Android的高校学生考勤系统的设计与实现
- Android 实现 iBeacon