Android解決虛擬按鍵欄遮擋問題
最近在公司的項(xiàng)目中 , 華為用戶反饋出了一個(gè)問題 , 華為手機(jī)底部有虛擬按鍵欄把應(yīng)用的底部?jī)?nèi)容遮擋住了 , 現(xiàn)在已經(jīng)把這個(gè)問題解決了 , 記錄一下,給各位遇到相同問題的童鞋做一下參考.
這里的解決方案還是相對(duì)比較簡(jiǎn)單的,首先判斷用戶的手機(jī)是否存在虛擬按鍵,若存在,那么就獲取虛擬按鍵的高度,然后再用代碼設(shè)置相同高度的TextView,這樣手機(jī)的虛擬按鍵就不會(huì)將底部的內(nèi)容遮擋住了。
處理虛擬按鍵欄工具類:
public class ScreenUtils {//獲取虛擬按鍵的高度public static int getNavigationBarHeight(Context context) {int result = 0;if (hasNavBar(context)) {Resources res = context.getResources();int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");if (resourceId > 0) {result = res.getDimensionPixelSize(resourceId);}}return result;}/*** 檢查是否存在虛擬按鍵欄** @param context* @return*/(Build.VERSION_CODES.ICE_CREAM_SANDWICH)public static boolean hasNavBar(Context context) {Resources res = context.getResources();//讀取系統(tǒng)資源函數(shù)int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");//獲取資源idif (resourceId != 0) {boolean hasNav = res.getBoolean(resourceId);// check override flagString sNavBarOverride = getNavBarOverride();if ("1".equals(sNavBarOverride)) {hasNav = false;} else if ("0".equals(sNavBarOverride)) {hasNav = true;}return hasNav;} else { // fallbackreturn !ViewConfiguration.get(context).hasPermanentMenuKey();}}/*** 判斷虛擬按鍵欄是否重寫* @return*/private static String getNavBarOverride() {String sNavBarOverride = null;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {try {Class c = Class.forName("android.os.SystemProperties");Method m = c.getDeclaredMethod("get", String.class);m.setAccessible(true);sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");} catch (Throwable e) {}}return sNavBarOverride;}}
調(diào)用工具類方法 , 獲取虛擬按鍵高度:
//處理虛擬按鍵//判斷用戶手機(jī)機(jī)型是否有虛擬按鍵欄if(ScreenUtils.hasNavBar(getApplicationContext())){setNavigationBar();}//處理虛擬按鍵private void setNavigationBar() {int barHeight = ScreenUtils.getNavigationBarHeight(getApplicationContext());LinearLayout.LayoutParams barParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);TextView tv = new TextView(this);tv.setHeight(barHeight);tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);tv.setBackgroundColor(Color.BLACK);llNavigationBar.addView(tv,barParams);}
到這里就結(jié)束啦!
評(píng)論
圖片
表情
