资讯

精准传达 • 有效沟通

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

android的状态栏,android 自定义状态栏

Android 完全隐藏状态栏方法

Android 完全隐藏状态栏方法  

成都创新互联专注于企业成都全网营销、网站重做改版、马边彝族网站定制设计、自适应品牌网站建设、H5页面制作商城开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为马边彝族等各大城市提供网站开发制作服务。

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

首先给出一张图:

2 2

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

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

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

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

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

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

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

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

Android去除状态栏的方法汇总

一、隐藏标题栏

//隐藏标题栏

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

二、隐藏状态栏

//隐藏状态栏

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

三、去掉所有Activity界面的标题栏

修改AndroidManifest.xml

在application 标签中添加android:theme="@android:style/Theme.NoTitleBar"

四、去掉所有Activity界面的TitleBar 和StatusBar

修改AndroidManifest.xml

在application 标签中添加

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

android导航栏与状态栏颜色及透明度

首先创建一个空项目,如下图

可以看到状态栏是白字黑背景, 导航栏也是白图标黑背景

嘿嘿, 我们先把状态栏隐藏掉,在添加一个ImageView, 让ImageView做背景(方便查看)

样子如下:

将状态栏和导航栏设置透明, 找到 Manifest.xml 文件, 在主题样式中修改

android:statusBarColor 设置状态栏背景色

android:navigationBarColor 同上

android:windowLightStatusBar 设置状态栏文字色, true为深色, false为白色

android:windowLightNavigationBar 同上

android:windowTranslucentStatus 设置状态栏半透明状态, true为半透明, false为不透明

android:windowTranslucentNavigation 同上

最后两个半透明状态下面没用, 可自己尝试看效果

效果图如下:

可以看到导航栏与状态栏并没有透明,原因是默认不能占用状态栏空间与导航栏空间,根布局背景为白色,所有这里显示白色

可以通过设置 getWindow().getDecorView().setSystemUiVisibility() 来适配

View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 适配状态栏空间

View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 适配导航栏空间

效果如下:

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 沉浸式状态栏

沉浸式状态栏是一种比较常见的UI风格,接下来就去看看怎么实现它。

在styles.xml里增加TranslucentTheme,我们这里minSdkVersion 是以21为准,低于安卓5.0以下的手机很少了,就不适配了。

对于这种没有标题栏,图片沉浸到状态栏的效果,我们已经实现了。如果是有标题栏呢?比如加个Toolbar会变成下面这样:

对于有标题的页面,我们希望状态栏颜色跟标题栏一样就行了,不希望标题栏上移跟状态栏重叠,我们可以在布局文件根视图设置如下属性,这个相当于设置了个padding让状态栏下移,当然,为了让状态栏颜色跟标题栏一样,你还需要给根视图设置一样的背景色(因为状态栏实际是透明的)。

运行看看,已经实现了我们的要求。


文章名称:android的状态栏,android 自定义状态栏
分享链接:http://cdkjz.cn/article/dsdjjgs.html
多年建站经验

多一份参考,总有益处

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

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

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