1. 程式人生 > >android適配華為菠菜源碼搭建虛擬鍵

android適配華為菠菜源碼搭建虛擬鍵

抽取 navig right 監聽器 instance 網上 alc except phi

在做菠菜源碼搭建 dsluntan.com VX:17061863513橫豎屏展示時,發現網上適配虛擬鍵代碼沒有做橫屏狀態下適配,導致橫屏狀態下,底部虛擬鍵遮擋了布局內容。

所以橫屏狀態下也需要適配華為虛擬鍵。只需要在content布局改變時,同時記錄當前可用的視圖寬度,重新請求布局即可。下面是代碼:

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;

import android.view.ViewTreeObserver;

import java.lang.reflect.Method;

public class NavigationBarUtil {

private static NavigationBarUtil Instance;

public static NavigationBarUtil getInstance() {
    return Instance = new NavigationBarUtil();
}

public void initActivity(View content) {
    mObserved = content;
    //給View添加全局的布局監聽器監聽視圖的變化
    mObserved.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            resetViewHeight();
        }
    });
    layoutParams = mObserved.getLayoutParams();
}

private View mObserved;//被監聽的視圖
private int usableHeightView;//視圖變化前的可用高度
private int usableWidthView;//視圖變化前的可用寬度
private ViewGroup.LayoutParams layoutParams;

private NavigationBarUtil() {
}

/**
 * 重置視圖的高度,使不被底部虛擬鍵遮擋
 */
private void resetViewHeight() {
    int usableHeightViewNow = CalculateAvailableHeight();
    int usableWidthViewNow = CalculateAvailableWidth();
    //比較布局變化前後的View的可用高度
    if (usableHeightViewNow != usableHeightView) {
        //如果兩次高度不一致
        //將當前的View的可用高度設置成View的實際高度
        layoutParams.height = usableHeightViewNow;
        mObserved.requestLayout();//請求重新布局
        usableHeightView = usableHeightViewNow;
    }
    if (usableWidthViewNow != usableWidthView) {
        layoutParams.width = usableWidthViewNow;
        mObserved.requestLayout();//請求重新布局
        usableWidthView = usableWidthViewNow;
    }
}

/**
 * 計算視圖的寬度
 * @return
 */
private int CalculateAvailableWidth() {
    Rect r = new Rect();
    mObserved.getWindowVisibleDisplayFrame(r);
    return (r.right);
}

/**
 * 計算視圖的高度
 *
 * @return
 */
private int CalculateAvailableHeight() {
    Rect r = new Rect();
    mObserved.getWindowVisibleDisplayFrame(r);

// return (r.bottom - r.top);//如果不是沈浸狀態欄,需要減去頂部高度
return (r.bottom);//如果是沈浸狀態欄
}

/**
 * 判斷底部是否有虛擬鍵
 *
 * @param context
 * @return
 */
public boolean hasNavigationBar(Context context) {
    boolean hasNavigationBar = false;
    Resources rs = context.getResources();
    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        hasNavigationBar = rs.getBoolean(id);
    }
    try {
        Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
        Method m = systemPropertiesClass.getMethod("get", String.class);
        String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
        if ("1".equals(navBarOverride)) {
            hasNavigationBar = false;
        } else if ("0".equals(navBarOverride)) {
            hasNavigationBar = true;
        }
    } catch (Exception e) {

    }
    return hasNavigationBar;

}

}
調用方式:在抽取的Activity基類中setContentView()方法後調用

    //適配華為部分機型虛擬鍵
    if (NavigationBarUtil.getInstance().hasNavigationBar(this)) {
        NavigationBarUtil.getInstance().initActivity(findViewById(android.R.id.content));
    }

android適配華為菠菜源碼搭建虛擬鍵