Android 4.0 隐藏虚拟按键(导航栏)的方法

原创 2012年03月28日 13:00:50

在Android4.0里,多了一些API,其中包括怎样隐藏虚拟按键(导航栏),这样编写游戏或一个视频播放器时,可以设置隐藏虚拟按键的参数,达到最佳效果,当然前提是使用的手机是有虚拟按键的,例如galaxy nexus(i9250)。好吧,看一看官方的Android4.0 api说明就明白了。


Controls for system UI visibility

Since the early days of Android, the system has managed a UI component known as the status bar, which resides at the top of handset devices to deliver information such as the carrier signal, time, notifications, and so on. Android 3.0 added the system bar for tablet devices, which resides at the bottom of the screen to provide system navigation controls (Home, Back, and so forth) and also an interface for elements traditionally provided by the status bar. In Android 4.0, the system provides a new type of system UI called the navigation bar. You might consider the navigation bar a re-tuned version of the system bar designed for handsets—it provides navigation controls for devices that don’t have hardware counterparts for navigating the system, but it leaves out the system bar's notification UI and setting controls. As such, a device that provides the navigation bar also has the status bar at the top.

To this day, you can hide the status bar on handsets using the FLAG_FULLSCREEN flag. In Android 4.0, the APIs that control the system bar’s visibility have been updated to better reflect the behavior of both the system bar and navigation bar:

  • The SYSTEM_UI_FLAG_LOW_PROFILE flag replaces the STATUS_BAR_HIDDEN flag. When set, this flag enables “low profile" mode for the system bar or navigation bar. Navigation buttons dim and other elements in the system bar also hide. Enabling this is useful for creating more immersive games without distraction for the system navigation buttons.
  • The SYSTEM_UI_FLAG_VISIBLE flag replaces the STATUS_BAR_VISIBLE flag to request the system bar or navigation bar be visible.
  • 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). The navigation bar returns to view as soon as the system receives user input. As such, this mode is useful primarily for video playback or other cases in which the whole screen is needed but user input is not required.

You can set each of these flags for the system bar and navigation bar by calling setSystemUiVisibility() on any view in your activity. The window manager combines (OR-together) all flags from all views in your window and apply them to the system UI as long as your window has input focus. When your window loses input focus (the user navigates away from your app, or a dialog appears), your flags cease to have effect. Similarly, if you remove those views from the view hierarchy their flags no longer apply.

To synchronize other events in your activity with visibility changes to the system UI (for example, hide the action bar or other UI controls when the system UI hides), you should register a View.OnSystemUiVisibilityChangeListener to be notified when the visibility of the system bar or navigation bar changes.

See the OverscanActivity class for a demonstration of different system UI options.


看完后,写个测试例子试一下,Ok~

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class HideTestActivity extends Activity implements OnClickListener{
	View main ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        main = getLayoutInflater().from(this).inflate(R.layout.main, null);
        main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        main.setOnClickListener(this);
        setContentView(main);
    }

	@Override
	public void onClick(View v) {
		int i = main.getSystemUiVisibility();
		if (i == View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) {
			main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
		} else if (i == View.SYSTEM_UI_FLAG_VISIBLE){
			main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
		} else if (i == View.SYSTEM_UI_FLAG_LOW_PROFILE) {
			main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
		}
	}
}


Android如何隐藏底部虚拟按键

虚拟按键隐藏相信大家在做屏幕适配时候,会对虚拟按键问题或多或少有一些疑问,接下来带给大家解决虚拟按键的方法。android隐藏底部虚拟键Navigation Bar实现全屏 安卓4.1之后为全虚拟...
  • qq_15807167
  • qq_15807167
  • 2016年06月10日 13:34
  • 9478

activity全屏实现沉浸式效果,并且单独触摸不会弹出虚拟按键

这种方法可以实现activity全屏,且不需要修改源码实现触摸不弹出NavigationBar的功能...
  • jx_shi
  • jx_shi
  • 2016年08月26日 16:12
  • 2785

Android适配底部虚拟按键的方法

最近项目进行适配的时候发现部分(如华为手机)存在底部虚拟按键的手机会因为虚拟按键的存在导致挡住部分界面,因为需要全屏显示,故调用虚拟按键隐藏方法使之隐藏,然而发现出现如下问题: 手动操作隐藏虚拟按键后...
  • yuanzhihui123
  • yuanzhihui123
  • 2017年04月11日 17:37
  • 4747

【Android】隐藏底部虚拟按键,亲测可用

Google的官方文档是:https://developer.android.com/training/system-ui/navigation.html#behind 示例代码 View dec...
  • u011511368
  • u011511368
  • 2016年10月10日 10:15
  • 14224

Android 如何做到隐藏手机底部虚拟按键?

现在的Android手机有好多底部带虚拟按键,比如华为、nexus、魅族等等,一般情况下对APP没啥影响,但是有时候就必须要强制隐藏。 例如:游戏的时候,拍照的时候。。。 接下来加上几个方法就OK...
  • woaiheima
  • woaiheima
  • 2017年03月14日 10:35
  • 5349

如何去除android对话框的导航栏

下面的sample解决了以下问题: 1.随意设置自定义对话框的位置 2.设置对话框的背景为透明(这样使得自定义的图标完全起作用) 3.弹出对话框时不会同时弹出导航栏。 4.去除对话框的标题栏(如果不去...
  • hhdsyxwei
  • hhdsyxwei
  • 2017年02月13日 18:19
  • 1312

Android底部虚拟按键遮挡应用

问题: 应用底部黑色部分被虚拟按键挡住了
  • baidu_32237719
  • baidu_32237719
  • 2016年12月28日 17:42
  • 2054

Android dialog 去除虚拟按键

Android 部分机型没有物理按键,只有虚拟按键,Activity全屏方法如下: 1、在 setContentView(R.layout.activity_main);之前添加 getWind...
  • shengyakun1
  • shengyakun1
  • 2017年01月07日 15:51
  • 2869

弹出dialog时,隐藏navigation bar的方法

http://stackoverflow.com/questions/23520892/unable-to-hide-navigation-bar-during-alertdialog-logindi...
  • u013835855
  • u013835855
  • 2016年02月25日 17:42
  • 2284

Android 自定义Dialog去除title导航栏

Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog....
  • mimi5821741
  • mimi5821741
  • 2015年07月27日 10:45
  • 1944
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android 4.0 隐藏虚拟按键(导航栏)的方法
举报原因:
原因补充:

(最多只允许输入30个字)