關於 Android4.0 隱藏虛擬按鍵的問題 實現全屏

Android 4.0 因爲項目需要, 要實現屏幕全屏,隱藏虛擬按鍵,即導航欄


在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要求導航欄完全隱藏-->但這對部分硬件設備有效


本人發現一個小問題:使用SYSTEM_UI_FLAG_HIDE_NAVIGATION時,會導致那個類,每個事件需要重複一次,比如:點擊事件需要點擊兩次,滑動事件需要滑動兩次。


方法:1

package com.example.button;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.View;  
import android.view.Window;  
import android.view.WindowManager;  
public class MainActivity extends Activity {  
  
    Window window;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  

        window = getWindow();  
        WindowManager.LayoutParams params = window.getAttributes();  
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;  
        window.setAttributes(params);  

  
        setContentView(R.layout.main);  
    }  
}  


方法:2

package com.example.button;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.Window;  
import android.view.WindowManager;  
import android.widget.Button;  
  
public class MainActivity extends Activity {  
  
    View main;  
    private Button btn;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        main = getLayoutInflater().from(this).inflate(R.layout.main, null);  
  
        btn = (Button) main.findViewById(R.id.btn);  
        btn.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                int i = main.getSystemUiVisibility();  
  
                if (i == View.SYSTEM_UI_FLAG_VISIBLE) {  
                    main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);  
                }  
            }  
        });  
        setContentView(main);  
    }  











發佈了38 篇原創文章 · 獲贊 43 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章