资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android状态栏,android状态栏挖孔屏怎样设置隐藏

Android如何设置顶部状态栏颜色(主题)

在Android中我们经常需要设置屏幕顶部状态栏的主题和应用页面保持同一风格,本文介绍几种常用的设置方案:

专注于为中小企业提供成都网站制作、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业富顺免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

首先给出一张图:

2 2

通过上图,我们可以通过设置不同的属性来达到控制不同位置颜色的目的,下面给出使用示例,修改res/values-19里面的内容:

主要是设置 colorPrimary,colorPrimaryDark这两个属性的值来设置状态栏的颜色,需要注意的是:

1:AndroidManifest.xml文件中的targetSdkVersion必须设置在 21 以上。

2.parent主题必须是 Theme.AppCompat 开头,兼容包下的主题,所以必须一用 v7 包。

在顶部标题栏设置属性值达到风格一致的目的

首先修改res/values-v19文件夹下的styles.xml文件内容如下(如果没有可以新建一个):

然后设置顶部标题控件的两个属性:

这时状态栏会保持与设置fitsSystemWindow属性的控件的背景颜色一致。

Android 状态栏的设置

先看一下默认的情况:

蓝色一行是自定义的导航栏,

黑色的是自带的 ActionBar ,也就是我们说的标题栏。

首先一般都会选择去掉 ActionBar:

隐藏 actionbar 有很多种方法

这种方法是全局中隐藏了标题栏。

其实在我的手机更新系统之前,隐藏了 ActionBar 后,状态栏和自定义的导航栏颜色是相匹配的,不知道什么原因现在默认为灰色了。

上面使用的主题虽然隐藏了标题栏,但是和我们自定义的导航栏不搭,

这时候我们可以选择用自定义的主题(Theme),来改变状态栏:

在 values 下的 style.xml 中添加

或者在 onCreate 中:

上面两行一般不一起设置,二选一即可。

第一行设置导航栏为透明,第二行将导航栏隐藏。

不推荐第二种做法,如果一个 Activity 中设置了隐藏导航栏而另一个 Activity 没有,那两者切换的时候会不好看。

融合的效果:

状态栏和 app 顶部相融合了,如果标题栏是一张图片效果会更好。

这里还有一个问题,状态栏的文字和我们导航栏的文字重叠了,

我们可以选择在布局文件的根元素中添加:

让布局为状态栏留出空间,就不会出现上面这张被状态栏遮挡的情况。

如果像上面的例子是一样的纯色的标题栏,我们可以选择直接改变状态栏的颜色解决问题。

或者:

不显示时间、电量等信息和文字:

同要可以用修改 Theme 来实现:

或者在 OnCreat() 中加入,还是要注意加在 setContentView() 的前面

如果想让图片全屏要注意设置为:

Android 完全隐藏状态栏方法

Android 完全隐藏状态栏方法  

1. 隐藏ActionBar:

ActionBar actionBar = getActionBar();

if (actionBar != null) {

actionBar.hide();

}

如果是继承AppCompatActivity,就用getSupportActionBar()。

2. 隐藏状态栏

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

通过这两个步就可以全屏显示启动页了。

然而,当开始动态申请权限,弹出系统的权限提示对话框后,状态栏又重新露出来了。我日,不是隐藏了吗?怎么又出来了,什么鬼?

通过查看源码的解释:

/**

* Request that the visibility of the status bar or other screen/window

* decorations be changed.

*

* pThis method is used to put the over device UI into temporary modes

* where the user's attention is focused more on the application content,

* by dimming or hiding surrounding system affordances.  This is typically

* used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY

* Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content

* to be placed behind the action bar (and with these flags other system

* affordances) so that smooth transitions between hiding and showing them

* can be done.

*

* pTwo representative examples of the use of system UI visibility is

* implementing a content browsing application (like a magazine reader)

* and a video playing application.

*

* pThe first code shows a typical implementation of a View in a content

* browsing application.  In this implementation, the application goes

* into a content-oriented mode by hiding the status bar and action bar,

* and putting the navigation elements into lights out mode.  The user can

* then interact with content while in this mode.  Such an application should

* provide an easy way for the user to toggle out of the mode (such as to

* check information in the status bar or access notifications).  In the

* implementation here, this is done simply by tapping on the content.

*

* {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java

*      content}

*

* pThis second code sample shows a typical implementation of a View

* in a video playing application.  In this situation, while the video is

* playing the application would like to go into a complete full-screen mode,

* to use as much of the display as possible for the video.  When in this state

* the user can not interact with the application; the system intercepts

* touching on the screen to pop the UI out of full screen mode.  See

* {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.

*

* {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java

*      content}

*

* @param visibility  Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},

* {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},

* {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},

* {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}, {@link #SYSTEM_UI_FLAG_IMMERSIVE},

* and {@link #SYSTEM_UI_FLAG_IMMERSIVE_STICKY}.

*/

从释义上可以知道,setSystemUiVisibility()是用于使系统UI进入一种临时的模式,目的是使用户的注意力关注于应用程序的内容上。所以单独一个Activity这样设置是可以全屏显示的,这个只对当前的Activity有效。可是当申请系统权限使,弹出的对话框是系统的Activity,通过adb shell dumpsys activity 来看,当前最顶端的Activity已经是GrantPermissionsActivity。

Run #2: ActivityRecord{2b99111 u0 com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity t141}

而这个GrantPermissionsActivity,我们并无法去设置它的setSystemUiVisibility()。所以这种方法不奏效。

通过和同事讨论,后来找到一种方法,可以实现我们的需求。

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

这种方法是OK的。

它的源码释义是:

/**

* Set the flags of the window, as per the

* {@link WindowManager.LayoutParams WindowManager.LayoutParams}

* flags.

*

* pNote that some flags must be set before the window decoration is

* created (by the first call to

* {@link #setContentView(View, android.view.ViewGroup.LayoutParams)} or

* {@link #getDecorView()}:

* {@link WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN} and

* {@link WindowManager.LayoutParams#FLAG_LAYOUT_INSET_DECOR}.  These

* will be set for you based on the {@link android.R.attr#windowIsFloating}

* attribute.

*

* @param flags The new window flags (see WindowManager.LayoutParams).

* @param mask Which of the window flag bits to modify.

* @see #addFlags

* @see #clearFlags

*/

public void setFlags(int flags, int mask) {}

仔细分析发现,这个是设置整个当前Window的,而setSystemUiVisibility()聚焦于显示Activity内容的,还是有差别的。

/**

* Window flag: hide all screen decorations (such as the status bar) while

* this window is displayed.  This allows the window to use the entire

* display space for itself -- the status bar will be hidden when

* an app window with this flag set is on the top layer. A fullscreen window

* will ignore a value of {@link #SOFT_INPUT_ADJUST_RESIZE} for the window's

* {@link #softInputMode} field; the window will stay fullscreen

* and will not resize.

*

* pThis flag can be controlled in your theme through the

* {@link android.R.attr#windowFullscreen} attribute; this attribute

* is automatically set for you in the standard fullscreen themes

* such as {@link android.R.style#Theme_NoTitleBar_Fullscreen},

* {@link android.R.style#Theme_Black_NoTitleBar_Fullscreen},

* {@link android.R.style#Theme_Light_NoTitleBar_Fullscreen},

* {@link android.R.style#Theme_Holo_NoActionBar_Fullscreen},

* {@link android.R.style#Theme_Holo_Light_NoActionBar_Fullscreen},

* {@link android.R.style#Theme_DeviceDefault_NoActionBar_Fullscreen}, and

* {@link android.R.style#Theme_DeviceDefault_Light_NoActionBar_Fullscreen}./p

*/

public static final int FLAG_FULLSCREEN      = 0x00000400;

从释义上得知,设置这个flag可以隐藏所有的屏幕修饰,像status bar,用于Window使用整个显示屏。这个完全是我们的目的了。

Android 状态栏透明

前言:最近项目大量用到状态栏透明,网上也出现很多库可以直接拿来用,个人认为没有必要那么重引用到一个库(有木有同学和我有一样的想法),所以研究了一番,在此做个记录加强记忆也便后期查阅,如果无意中有幸能帮助到你那就再好不过了。

Android 从 4.4 (SDK 19) 开始支持 系统栏(状态栏+导航栏)半透明 效果:

翻译一下就是:

TranslucentDecor 主题设置了两个属性 windowTranslucentStatus 和 windowTranslucentNavigation 都为 true,前者指定状态栏半透明、后者指定导航栏半透明。

本文只探讨“状态栏” 。

默认样式是这样:

可见 Toolbar 和系统状态栏之间有明显的分界,我们要实现的效果是 Toolbar 和状态栏背景统一,看起来像是一个整体(自行脑补图片)。

按照官方文档,我们自定义主题:

对应的 Activity 引用该主题:

我看来看看效果:

虽然实现了半透明,但是布局被状态栏覆盖,接下来在布局文件中设置 fitSystemWindows (注意加到根节点 ConstraintLayout 上):

来看看效果:

虽然布局没有被状态栏覆盖,但是状态栏背景显然这不是我们想要的效果????

为什么状态栏会这么奇怪?

文章开头的定义中我们说了,布局文件会延伸到状态栏所占区域下, fitsSystemWindows 的作用是给对应的 View 增加 padding(这里以 ConstraintLayout 为例),目的是为了让其内容不被状态栏遮挡。

在我们的布局文件中 ConstraintLayout 没有设置背景(默认白色),所以状态栏默认的半透明背景色和 ConstraintLayout 的白色背景叠加,就变成了上图中的效果。

【总结】两个基本概念:

1、 windowTranslucentStatus 设置为true之后,状态栏默认是 半透明 的(4.4 是黑色到透明色渐变,5.0+ 是纯黑色半透明),和我们要求的 透明 相去甚远。更重要的是,布局会延伸到状态栏底下。

2、 android:fitsSystemWindows 简单理解 就是 View 为了适配系统状态栏和导航栏(不被遮挡)自动 增加 padding ,当然真正的实现原理比这复杂很多而且不同的 View 可以自定义实现方式。

所以,为了实现文章开头提出来的“状态栏透明”效果,我们需要处理:

设置 windowTranslucentStatus 为 true,让状态栏半透明。

在根节点设置 android:fitsSystemWindows 使其不被状态栏遮挡。

Android 4.4 暂时没有办法去掉状态栏的渐变。

Android 5.0+ 开始支持修改状态栏颜色,设置透明色即可把半透明去掉。

看看效果:

我们看到即使状态栏透明了,但是其底色是一片白,因为跟节点 ConstraintLayout 没有设置背景,大多情况下我们不会给整个跟节点设置颜色,可以考虑把 android:fitsSystemWindows 设置到子 View 上,本例中是 AppBarLayout (5.0+ 无效,只能显式给 AppBarLayout 加 padding,可以利用其背景色),实际项目中可灵活调整。

最终效果:

至此,完成状态栏透明效果,网上有很多库,实际上都是基于此原理,在此基础上再自定义 View 做为状态栏背景。

Android设置状态栏颜色和状态栏文字、图标颜色

Android开发中,经常需要实现下图状态栏的效果,类似于沉浸式状态栏,但这里仅仅是讨论设置状态栏的颜色和状态栏上面文字、图标的颜色的方法。

Android 4.4(API 19)之后,就提供了修改状态栏颜色的方法,但是在 Android 6.0(API 23)之后,才支持修改状态栏上面的文字和图标颜色,默认是白色的。所以会导致一个问题,在 4.4 到 6.0 之间的系统,状态栏设置为浅色的话,状态栏上面白色的文字和图标会看不清,像下面这样:

有一些第三方的系统提供了设置状态栏和状态栏文字、图标颜色的方法,比如小米的MIUI和魅族的Flyme,所以考虑了下比较好的实现方式是:

当然,这里面也会有坑,比如 MIUI 提供的修改状态栏字体颜色方法会跟 Android 系统自带的方法冲突,官方说明如下: 关于MIUI状态栏字符颜色逻辑调整说明

经过网上的资料和自己的尝试,MIUI 系统还是同时使用 MIUI 提供的方法和 Android 系统自带的方法来修改状态栏字体颜色比较保险。

基于上面的思考,封装了设置 Android 4.4 以上系统状态栏颜色和状态栏字体、图标颜色的方法:

要在 Application Theme 加上 item name="android:fitsSystemWindows"true/item ,不然页面会顶到状态栏上面,或者在 Activity 的布局里面加上 android:fitsSystemWindows="true" 和 android:clipToPadding="false" 也可以。

最终实现的效果如下:

大家有更好的方案可以告诉我~

Android 沉浸式/透明式状态栏、导航栏

Android 从4.4开始引进透明状态栏和导航栏的概念,并且在5.0进行了改进,将透明变成了半透明的效果。虽然此特性最早出现在ios,但不否认效果还是很赞的。

至于4.4以下的手机,就不要考虑此特性了,好在4.4以下的手机份额已经非常小了。

我们先来看一下透明状态栏的实现,两种常见效果图如下:

虚拟导航栏并不是所有的手机都有,华为的手机多比较常见,就是上图屏幕底部按钮那块区域。设置导航栏和状态栏类似:

这是官方的解释,大致意思就是我们在布局的最外层设置 android:fitsSystemWindows="true",会在屏幕最上方预留出状态栏高度的padding。

由于fitsSystemWindows属性本质上是给当前控件设置了一个padding,所以我们设置到根布局的话,会导致状态栏是透明的,并且和窗口背景一样。

但是多数情况,我们并不在根布局设置这个属性,我们想要的无外乎是让内容沉浸在状态栏之中。所以我们经常设置在最上端的图片背景、Banner之类的,如果是Toolbar的,我们可以使用一层LinearLayout包裹,并把这个属性设置给LinearLayout,这样就可以避免Toolbar的内容下沉了。如:

上述方法可以解决普通页面的透明式状态栏需求,如有复杂需求可以参考下面这些:

Android 系统状态栏沉浸式/透明化完整解决方案

Android 沉浸式状态栏的实现

Android沉浸式状态栏(透明状态栏)最佳实现

还有开源库推荐: ImmersionBar


网站栏目:android状态栏,android状态栏挖孔屏怎样设置隐藏
URL分享:http://cdkjz.cn/article/dsceijo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220