如何在android活动中永久隐藏导航栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21724420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide navigation bar permanently in android activity?
提问by Sujith Manjavana
I want to hide navigation bar permanently in my activity(not whole system ui). now i'm using this piece of code
我想在我的活动中永久隐藏导航栏(不是整个系统用户界面)。现在我正在使用这段代码
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
It hides the bar but when user touches the screen it showing again. is there any way to hide it permanently until activity onStop()
;
它隐藏了栏,但当用户触摸屏幕时,它会再次显示。有没有办法在活动之前永久隐藏它onStop()
;
回答by Dawid Drozd
Snippets:
片段:
HideNavigationBarComponent.java
This is for Android 4.4+
这适用于 Android 4.4+
Try out immersive mode https://developer.android.com/training/system-ui/immersive.html
尝试沉浸式模式https://developer.android.com/training/system-ui/immersive.html
Fast snippet (for an Activityclass):
快速片段(对于Activity类):
private int currentApiVersion;
@Override
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
currentApiVersion = android.os.Build.VERSION.SDK_INT;
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
// This work only for android 4.4+
if(currentApiVersion >= Build.VERSION_CODES.KITKAT)
{
getWindow().getDecorView().setSystemUiVisibility(flags);
// Code below is to handle presses of Volume up or Volume down.
// Without this, after pressing volume buttons, the navigation bar will
// show up and won't hide
final View decorView = getWindow().getDecorView();
decorView
.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
{
@Override
public void onSystemUiVisibilityChange(int visibility)
{
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
{
decorView.setSystemUiVisibility(flags);
}
}
});
}
}
@SuppressLint("NewApi")
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if(currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
{
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
If you have problems when you press Volume up or Volume down that your navigation bar show. I added code in onCreate
see setOnSystemUiVisibilityChangeListener
如果您在按下导航栏显示的调高音量或调低音量时遇到问题。我添加的代码onCreate
见setOnSystemUiVisibilityChangeListener
Here is another related question:
这是另一个相关问题:
Immersive mode navigation becomes sticky after volume press or minimise-restore
回答by Evish Verma
Do this.
做这个。
public void FullScreencall() {
if(Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if(Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
This works 100% and you can do same for lower API versions, even if it's a late answer I hope it will helps someone else.
这 100% 有效,你可以对较低的 API 版本做同样的事情,即使它是一个迟到的答案,我希望它会帮助别人。
If you want this to be permanent, just call FullscreenCall()
inside your onResume()
method.
如果您希望这是永久性的,只需FullscreenCall()
在您的onResume()
方法中调用即可。
回答by Sandeep R
According to Android Developer site
I think you cant(as far as i know) hide navigation bar permanently..
我认为你不能(据我所知)永久隐藏导航栏..
However you can do one trick. Its a trick mind you.
但是你可以做一招。这是一个技巧。
Just when the navigation bar
shows up when user touches the screen. Immediately hide it again.
Its fun.
就在navigation bar
用户触摸屏幕时出现。立马又藏起来。好有趣。
Check this.
检查这个。
void setNavVisibility(boolean visible) {
int newVis = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| SYSTEM_UI_FLAG_LAYOUT_STABLE;
if (!visible) {
newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN
| SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// If we are now visible, schedule a timer for us to go invisible.
if (visible) {
Handler h = getHandler();
if (h != null) {
h.removeCallbacks(mNavHider);
if (!mMenusOpen && !mPaused) {
// If the menus are open or play is paused, we will not auto-hide.
h.postDelayed(mNavHider, 1500);
}
}
}
// Set the new desired visibility.
setSystemUiVisibility(newVis);
mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE);
mPlayButton.setVisibility(visible ? VISIBLE : INVISIBLE);
mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE);
}
See this for more information on this ..
有关此的更多信息,请参阅此 ..
回答by Namrata
Use:-
用:-
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
In Tablets running Android 4+, it is not possible to hide the System / Navigation Bar.
在运行 Android 4+ 的平板电脑中,无法隐藏系统/导航栏。
From documentation :
从文档:
The SYSTEM_UI_FLAG_HIDE_NAVIGATION is a new flag that requests the navigation bar hide completely. Be aware that this works only for the navigation bar used by some handsets (it does not hide the system bar on tablets).
SYSTEM_UI_FLAG_HIDE_NAVIGATION 是一个请求导航栏完全隐藏的新标志。请注意,这仅适用于某些手机使用的导航栏(它不会隐藏平板电脑上的系统栏)。
回答by Harsha Vardhan
For people looking at a simpler solution, I think you can just have this one line of code in
onStart()
对于寻找更简单解决方案的人,我认为您可以在其中添加一行代码
onStart()
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
It's called Immersive mode. You may look at the official documentation for other possibilities.
它被称为沉浸模式。您可以查看官方文档以了解其他可能性。
回答by PeerNet
It's my solution:
这是我的解决方案:
First, define boolean that indicate if navigation bar is visible or not.
首先,定义指示导航栏是否可见的布尔值。
boolean navigationBarVisibility = true //because it's visible when activity is created
Second create method that hide navigation bar.
隐藏导航栏的第二种创建方法。
private void setNavigationBarVisibility(boolean visibility){
if(visibility){
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
navigationBarVisibility = false;
}
else
navigationBarVisibility = true;
}
By default, if you click to activity after hide navigation bar, navigation bar will be visible. So we got it's state if it visible we will hide it.
默认情况下,如果您在隐藏导航栏后单击活动,导航栏将可见。所以我们得到它的状态,如果它可见,我们将隐藏它。
Now set OnClickListener to your view. I use a surfaceview so for me:
现在将 OnClickListener 设置为您的视图。我使用表面视图,所以对我来说:
playerSurface.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setNavigationBarVisibility(navigationBarVisibility);
}
});
Also, we must call this method when activity is launched. Because we want hide it at the beginning.
此外,我们必须在启动活动时调用此方法。因为我们想在一开始就隐藏它。
setNavigationBarVisibility(navigationBarVisibility);
回答by swisscoder
Its a security issue: https://stackoverflow.com/a/12605313/1303691
这是一个安全问题:https: //stackoverflow.com/a/12605313/1303691
therefore its not possible to hide the Navigation on a tablet permanently with one single call at the beginning of the view creation. It will be hidden, but it will pop up when touching the Screen. So just the second touch to your screen can cause a onClickEvent on your layout. Therefore you need to intercept this call, but i haven't managed it yet, i will update my answer when i found it out. Or do you now the answer already?
因此,不可能在视图创建开始时通过一次调用永久隐藏平板电脑上的导航。它将被隐藏,但在触摸屏幕时会弹出。因此,对屏幕的第二次触摸可能会导致布局上出现 onClickEvent。因此你需要拦截这个电话,但我还没有管理它,当我发现它时我会更新我的答案。或者你现在已经有了答案?
回答by qingfei song
I think the blow code will help you, and add those code before setContentView()
我认为打击代码会帮助你,并在 setContentView() 之前添加这些代码
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
Add those code behind setContentView() getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
在 setContentView() getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); 后面添加这些代码;
回答by Rashad
Try this:
尝试这个:
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
回答by Sabarulla
I think this code will resolve your issue. Copy and paste this code on your MainActivity.java
我认为此代码将解决您的问题。将此代码复制并粘贴到 MainActivity.java 上
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
hideNavigationBar();
}
}
});
}
private void hideNavigationBar() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
It will works on Android-10. I hope it will helps.
它适用于 Android-10。我希望它会有所帮助。