扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
一、隐藏标题栏
成都创新互联公司是一家专注于网站设计制作、网站设计与策划设计,常山网站建设哪家好?成都创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:常山等地区。常山做网站价格咨询:028-86922220
复制代码代码如下:
//隐藏标题栏
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"
[img]先看一下默认的情况:
蓝色一行是自定义的导航栏,
黑色的是自带的 ActionBar ,也就是我们说的标题栏。
首先一般都会选择去掉 ActionBar:
隐藏 actionbar 有很多种方法
这种方法是全局中隐藏了标题栏。
其实在我的手机更新系统之前,隐藏了 ActionBar 后,状态栏和自定义的导航栏颜色是相匹配的,不知道什么原因现在默认为灰色了。
上面使用的主题虽然隐藏了标题栏,但是和我们自定义的导航栏不搭,
这时候我们可以选择用自定义的主题(Theme),来改变状态栏:
在 values 下的 style.xml 中添加
或者在 onCreate 中:
上面两行一般不一起设置,二选一即可。
第一行设置导航栏为透明,第二行将导航栏隐藏。
不推荐第二种做法,如果一个 Activity 中设置了隐藏导航栏而另一个 Activity 没有,那两者切换的时候会不好看。
融合的效果:
状态栏和 app 顶部相融合了,如果标题栏是一张图片效果会更好。
这里还有一个问题,状态栏的文字和我们导航栏的文字重叠了,
我们可以选择在布局文件的根元素中添加:
让布局为状态栏留出空间,就不会出现上面这张被状态栏遮挡的情况。
如果像上面的例子是一样的纯色的标题栏,我们可以选择直接改变状态栏的颜色解决问题。
或者:
不显示时间、电量等信息和文字:
同要可以用修改 Theme 来实现:
或者在 OnCreat() 中加入,还是要注意加在 setContentView() 的前面
如果想让图片全屏要注意设置为:
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使用整个显示屏。这个完全是我们的目的了。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流