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

來源:互聯網
上載者:User

標籤:pre   exception   graphics   tco   記錄   ext.get   otto   global   method   

在做菠菜源碼搭建 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適配華為菠菜源碼搭建虛擬鍵

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.