Android 屏蔽导航栏方法

设定全屏时一些有虚拟键导航栏的手机导航栏还是在的,如果要去掉就必须用特殊方法了。如下:


import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.mgyun.general.helper.Logger;

/**
 * Created by AndyMao on 2015/12/21.
 */
public class NavigationBarHelp {


    /**
     * 是否有虚拟键
     *
     * @return
     */
    public static boolean hasNavigationBar(Context context) {
        boolean hasMenuKey = true, hasBackKey = true;
        boolean ret = false;

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
                hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            }

            if ((!hasMenuKey) && (!hasBackKey)) {
                ret = true;
            }
        } catch (Exception e) {
            ret = false;
        }

        return ret;
    }

    /**
     * 隐藏虚拟键
     */
    public static void hideNavigation(Activity context) {

        if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB)) {
            Logger.get().d("myth hideNavigation  " + context.getClass().getSimpleName());

            context.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE);
        }
    }

    /**
     * 隐藏虚拟键
     */
    public static void hideNavigation(View view) {

        if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB)) {
            Logger.get().d("myth hideNavigation  " + view.getClass().getSimpleName());

//            | View.SYSTEM_UI_FLAG_FULLSCREEN

            view.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_IMMERSIVE);
        }
    }

    /**
     * 显示虚拟键
     */
    public static void showNavigation(View view) {

        if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB)) {
            Logger.get().d("myth hideNavigation  " + view.getClass().getSimpleName());

            view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
    }


}
找到一些方法对导航栏显示及隐藏操作的。但有的手机可能还是没有用,因为导航栏会自动再出来。这是后可以判断自己view的的宽高,和屏幕宽高比,不一样再调一次这个方法就行了。

   @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int height = b - t;

        if (height > mMaxLayoutHeight) {
            mMaxLayoutHeight = height;
        }

        if ((mMaxLayoutHeight > height) && (null != mOnLayoutHeightChangeListener)) {
            mOnLayoutHeightChangeListener.OnLayoutHeightChange();
        }

     }

我用的就是在view中重写onLayout方法,纪录最大高度,不对就回调监听,里面执行隐藏导航栏的方法。


你可能感兴趣的:(Android开发问题系列)