Android4.0 隐藏虚拟按键 实现全屏

Android 想要实现屏幕全屏,隐藏虚拟按键,即导航栏。

1:使用SYSTEM_UI_FLAG_HIDE_NAVIGATION隐藏虚拟按键,可是再次触摸会唤醒虚拟按键。

2:SYSTEM_UI_FLAG_HIDE_NAVIGATION  把虚拟按键弄成小圆点。

在Android的API 中

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
 SYSTEM_UI_FLAG_LOW_PROFILE 相当于隐藏导航栏
 SYSTEM_UI_FLAG_VISIBLE         导航栏显示
 SYSTEM_UI_FLAG_HIDE_NAVIGATION 要求导航栏完全隐藏-->但这对部分硬件设备有效


方法一:

[java]  view plain copy
  1. package com.example.setbutton;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.View;  
  6. import android.view.Window;  
  7. import android.view.WindowManager;  
  8. public class MainActivity extends Activity {  
  9.   
  10.     Window window;  
  11.   
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         // main = getLayoutInflater().from(this).inflate(R.layout.main, null);  
  17.         window = getWindow();  
  18.         WindowManager.LayoutParams params = window.getAttributes();  
  19.         params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;  
  20.         window.setAttributes(params);  
  21.   
  22.         setContentView(R.layout.main);  
  23.     }  
  24.   
  25. }  

方法二: 

[java]  view plain copy
  1. package com.example.setbutton;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.Window;  
  8. import android.view.WindowManager;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     View main;  
  14.     private Button btn;  
  15.   
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         main = getLayoutInflater().from(this).inflate(R.layout.main, null);  
  21.   
  22.         btn = (Button) main.findViewById(R.id.btn);  
  23.         btn.setOnClickListener(new OnClickListener() {  
  24.   
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 // TODO Auto-generated method stub  
  28.                 int i = main.getSystemUiVisibility();  
  29.   
  30.                 if (i == View.SYSTEM_UI_FLAG_VISIBLE) {  
  31.                     main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);  
  32.                 }  
  33.             }  
  34.         });  
  35.   
  36.         setContentView(main);  
  37.   
  38.     }  

你可能感兴趣的:(Android开发)