Android 4.4 沉浸式状态栏的实现

article/2025/8/23 18:12:39

Android 4.4(kitkat)之后,Android Window 提供一个新的属性:

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
从名字上可以看出,这个属性可以用来设置状态栏是否透明,我们就可以利用这个属性来实现沉浸式的状态栏。沉浸式的实现跟布局有关,大致分为三种情况:

一、状态栏与页面大背景融合的沉浸式

    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);setImmerseLayout();}
    protected void setImmerseLayout() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {Window window = getWindow();window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);}}
在Android 4.4 以上设置了FLAG_TRANSLUCENT_STATUS 属性,从图上看实际的效果就是界面背景填充了状态栏,这样就是实现了状态栏与页面大背景融合的沉浸式。

二、带有自定义ToolBar的沉浸式

带有自定义ToolBar的沉浸式根据布局是否有EditText分为两种情况:

1.没有EditText


先来看看xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"><FrameLayoutandroid:id="@+id/fl_toolbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:background="#00cbd0"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"><ImageViewandroid:id="@+id/msg_second_back"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerVertical="true"android:paddingBottom="15dp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="15dp"android:scaleType="centerInside"android:src="@drawable/hu_back_normal" /><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerInParent="true"android:gravity="center"android:text="@string/app_name"android:textColor="#FFFFFF"android:textSize="22dp" /></RelativeLayout></FrameLayout>
</RelativeLayout>
是不是觉得代码中ID为fl_toolbar的FrameLayout有点多余,恩,这是有用的。如果单单调用setImmerseLayout()方法,toolbar就会直接融合到状态栏上(跟第一种情况是一样的)。避免这种情况,我们可设置Pandding(内边距)来使状态栏和toolbar不重叠,FrameLayout在这里发挥作用了。代码如下:

    protected void setImmerseLayout(View view) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {Window window = getWindow();window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);int statusBarHeight = getStatusBarHeight(this.getBaseContext());view.setPadding(0, statusBarHeight, 0, 0);}}
这样就可以设置Pandding了,pandding的高度应该就是状态栏的高度了。国内的rom的状态栏高度没有统一的标准(MIUI高些,Flyme矮些),所以只能动态获取,就是getStatusBarHeight方法。

    /*** 用于获取状态栏的高度。 使用Resource对象获取** @return 返回状态栏高度的像素值。*/private int getStatusBarHeight(Context context) {int result = 0;int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen","android");if (resourceId > 0) {result = context.getResources().getDimensionPixelSize(resourceId);}return result;}

2.含有EditText

含有EditText的情况就比较特殊了,设置了状态栏透明属性,导致在软键盘弹出后页面没有resize,内容被键盘遮住,adjustResize或者ScrollView不起作用,这时候需要fitSystemWindows属性来解决问题。

fitSystemWindows属性

    官方描述:

        Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.

    简单描述:

     这个一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为true,就会调整view的paingding属性来给system windows留出空间....

    实际效果:

     当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。

软键盘弹出后不遮挡内容,要求界面不能设置全屏,因此我们不能将fitSystemWindows属性设置在根layout,直接在需要relayout的子view上添加fitsSystemWindows属性即可。

<ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:fadingEdge="none"android:overScrollMode="never"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="50dp"android:layout_marginRight="50dp"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="150dp"android:background="#FFFFFF"android:orientation="vertical">...<EditTextandroid:layout_width="match_parent"android:layout_height="44dp"android:layout_marginLeft="15dp"android:layout_marginRight="15dp"android:background="@null"android:singleLine="true"android:textSize="14sp" /></LinearLayout></LinearLayout></ScrollView>

效果图如下:

需要注意的一点是设置fitSystemWindows的view 的padding属性会失效。有暂时的解决方法是将fitSystemWindows属性设置在xml根layout,并且设置适当的背景颜色。例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#00cbd0"android:fitsSystemWindows="true"android:orientation="vertical"><FrameLayoutandroid:id="@+id/score_login_topbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#00cbd0">

以上就是Android 沉浸式的实际实现过程,另外需要注意的一点就是存在父Activity包含子Activity的情况的话(如:ActivityGroup),父Activity耶必须设置状态栏透明属性,即调用setImmerseLayout()方法,才能实现子Activity的沉浸式状态栏。

Demo下载http://download.csdn.net/detail/a496263987/9443821


http://chatgpt.dhexx.cn/article/F0GYiHcW.shtml

相关文章

总结系列-一文搞懂沉浸式状态栏

近期做到与状态栏相关一些需求,网上关于沉浸式状态栏的文章有很多&#xff0c;基本上都先讲一堆概念&#xff0c;然后接着推出一个自己写的轮子,这类轮子面对很多不同场景的情况不能百分之百满足使用需求,过度地使用轮子也往往会让开发者不了解代码到底是怎么实现沉浸式的,基于…

安卓沉浸式状态栏

安卓沉浸式状态栏 前言安卓版本Android4.4之前Android4.4&#xff08;API 19&#xff09; - Android 5.0&#xff08;API 21&#xff09;1.图片沉浸1.1 xml实现1.2代码实现 2.ToolBar沉浸2.1xml实现2.1.1 设置 fitsSystemWindows 属性2.1.2 布局里添加占位状态栏 2.2代码实现2.…

Android-StatusBarUtil沉浸式状态栏

文章目录 一、前言二、去掉标题栏三、StatusBarUtil属性四、沉浸状态栏颜色五、沉浸状态栏图片 一、前言 大家再开发过程中会遇到上面这种情况&#xff0c;它不影响使用但是在美观上面差点意思&#xff0c;接下来教给大家一种方式来使它美观 二、去掉标题栏 只需要改变NoAction…

沉浸式状态栏(一)

我们知道IOS上的应用&#xff0c;状态栏的颜色总能与应用标题栏颜色保持一致&#xff0c;用户体验很不错&#xff0c;那安卓是否可以呢&#xff1f;若是在安卓4.4之前&#xff0c;答案是否定的&#xff0c;但在4.4之后&#xff0c;谷歌允许开发者自定义状态栏背景颜色啦&#x…

安卓沉浸式状态栏,android沉浸式状态栏工具类封装

文章目录 前言二、使用步骤1.定义2.使用3.效果 前言 沉浸式状态栏对安卓6.0以上版本很简单&#xff0c;今天分享一个封装好的工具&#xff0c;供大家参考 二、使用步骤 1.定义 代码如下&#xff1a; package com.example.mystudy_kotlin.utilsimport android.app.Activity …

沉浸式状态栏实现

文章目录 郭霖的博客这样实现透明状态栏隐藏导航栏真正的沉浸式模式 轮子 郭霖的博客这样实现 Android状态栏微技巧&#xff0c;带你真正理解沉浸式模式 透明状态栏 新建一个项目&#xff0c;只放一张图片 可以看到 状态栏、ActionBar、底部导航栏都显示&#xff0c;现在修…

flutter沉浸式状态栏

方法1&#xff1a;修改MainActivity 在MainActivity.kt或MainActivity.java&#xff0c;判断一下版本号然后将状态栏颜色修改设置成透明&#xff0c;因为他本身是黑色半透明&#xff1a; Kotlin&#xff1a; class MainActivity: FlutterActivity() {override fun configure…

uniapp 沉浸式状态栏

uniapp 沉浸式状态栏 1.page.json 中设置 &#xff1a;“navigationStyle”:"custom" "globalStyle": {"navigationBarTextStyle": "black","navigationBarTitleText": "uni-app","navigationStyle":…

Android使用沉浸式状态栏

Android使用沉浸式状态栏 为什么使用? 我们App里面目前都没有做沉浸式状态栏&#xff0c;会导致状态栏呈黑色条状&#xff0c;而且下面这个的黑色条状与App红色主界面有很明显的区别。这样在一定程度上牺牲了视觉高度&#xff0c;界面面积变小。 可以对照比较这三张图 代码…

【Android实战】沉浸式状态栏实现(上)

传统的手机状态栏是呈现出黑色条状的&#xff0c;有的和手机主界面有很明显的区别。这样就在一定程度上牺牲了视觉宽度&#xff0c;界面面积变小。 沉浸模式的状态栏和主界面完全融为了一体&#xff0c;在设计上有不同的视觉感受。 我们先上两张图&#xff0c;很容易看出区别&a…

Android 沉浸式状态栏

文章目录 Android 沉浸式状态栏前提情况一&#xff1a;使用FrameLayout情况二&#xff1a;使用CoordinatorLayoutfitsSystemWindows属性原理情况三&#xff1a;在CoordinatorLayout中添加子控件问题&#xff1a;解决&#xff1a; 情况四&#xff1a;使用FrameLayout实现沉浸式效…

Android实现沉浸式状态栏效果

关于沉浸式状态栏&#xff0c;给大家推荐一个非常好的博文android标题栏、状态栏图标文字颜色及背景动态变化 另外说明下&#xff0c;沉浸式状态栏的实现仅适用于 android 4.4及以上版本&#xff0c;4.4以下的就不要想了。 1. 实现秀明状态栏常规方法 //是否使用特殊的标题栏背…

适配“沉浸式”状态栏

传送门&#xff1a; fitSystemWindow属性的作用 http://blog.csdn.net/wangxp423/article/details/79564244 fitSystemWindow属性实战 http://blog.csdn.net/wangxp423/article/details/79566465 上两篇我们讲了fitSystemWindows实现沉浸式状态栏&#xff0c;本篇主要讲解适…

Android 实现沉浸式状态栏(包含顶部栏吸顶Layout CoordinatorLayout实现沉浸式状态栏)

前言 Android状态栏默认是固定的黑底白字&#xff0c;这肯定是不被伟大的设计师所喜爱的&#xff0c;更有甚者&#xff0c;某些时候设计希望内容能够延伸到状态栏上部&#xff08;例如顶部是大图的情况&#xff09;。所幸的是随着Android版本的迭代&#xff0c;开发者对状态栏…

Android实现ImmersionBar沉浸式状态栏

&#xff08;一&#xff09;效果图 &#xff08;二&#xff09;实现步骤&#xff1a; 1、在build.gradle中加上 implementation com.gyf.barlibrary:barlibrary:2.3.0 2、设置页面为全屏 将上图中的 <style name"AppTheme" parent"Theme.AppCompat.Light.…

Android沉浸式状态栏实现

首先创建一个BaseActivity或者BaseFragment&#xff0c; 后面需要沉浸式状态的继承BaseActivity或BaseFragment, 然后在XML文件中添加想设置的状态栏背景颜色&#xff0c;以下两句代码 android:background"#1677FE" android:fitsSystemWindows“true” BaseActivity代…

一个Android沉浸式状态栏上的黑科技

本文同步发表于我的微信公众号&#xff0c;扫一扫文章底部的二维码或在微信搜索 郭霖 即可关注&#xff0c;每个工作日都有文章更新。 说起来&#xff0c;在不知不觉中&#xff0c;我竟然凑成了这沉浸式状态栏三部曲。 其实最开始的时候&#xff0c;我主要是因为工作上的原因想…

Android 实现沉浸式状态栏

上一篇文章将Android 实现变色状态栏我们实现了变色的状态栏&#xff0c;也介绍了沉浸式状态栏和透明状态栏的区别&#xff0c;这篇文章我们实现沉浸式状态栏。 沉浸式状态栏的来源就是很多手机用的是实体按键&#xff0c;没有虚拟键&#xff0c;于是开了沉浸模式就只有状态栏消…

Android 沉浸式状态栏攻略 让你的状态栏变色吧

转载请标明出处&#xff1a; http://blog.csdn.net/lmj623565791/article/details/48649563&#xff1b; 本文出自:【张鸿洋的博客】 一、概述 近期注意到QQ新版使用了沉浸式状态栏&#xff0c;ok&#xff0c;先声明一下&#xff1a;本篇博客效果下图&#xff1a; 关于这个状…

Android 最新实现沉浸式状态栏的效果

博主前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住也分享一下给大家&#xff0c; &#x1f449;点击跳转到网站 什么是状态栏: 是指手机屏幕最顶上&#xff0c;显示中国移动、安全卫士、电量、网速等等&#xff0c;在手…