Android如何解決虛擬按鍵欄遮擋問題
最近在公司的項目中 , 華為用戶反饋出了一個問題 , 華為手機底部有虛擬按鍵欄把應用的底部內(nèi)容遮擋住了 , 現(xiàn)在已經(jīng)把這個問題解決了 , 記錄一下,給各位遇到相同問題的童鞋做一下參考.
這里的解決方案還是相對比較簡單的,首先判斷用戶的手機是否存在虛擬按鍵,若存在,那么就獲取虛擬按鍵的高度,然后再用代碼設置相同高度的TextView,這樣手機的虛擬按鍵就不會將底部的內(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
*/
@TargetApi(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");//獲取資源id
if (resourceId != 0) {
boolean hasNav = res.getBoolean(resourceId);
// check override flag
String sNavBarOverride = getNavBarOverride();
if ("1".equals(sNavBarOverride)) {
hasNav = false;
} else if ("0".equals(sNavBarOverride)) {
hasNav = true;
}
return hasNav;
} else { // fallback
return !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)用工具類方法 , 獲取虛擬按鍵高度:
//處理虛擬按鍵
//判斷用戶手機機型是否有虛擬按鍵欄
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é)束啦!
以上就是Android如何解決虛擬按鍵欄遮擋問題的詳細內(nèi)容,更多關于Android 虛擬按鍵欄遮擋的資料請關注腳本之家其它相關文章!
相關文章
Android 系統(tǒng)net和wap接入點的區(qū)別
這篇文章主要介紹了Android 系統(tǒng)net和wap接入點的區(qū)別的相關資料,需要的朋友可以參考下2016-09-09
詳解android 用webview加載網(wǎng)頁(https和http)
這篇文章主要介紹了詳解android 用webview加載網(wǎng)頁(https和http),詳細的介紹了兩個錯誤的解決方法,有興趣的可以了解一下2017-11-11
Android 5.1 WebView內(nèi)存泄漏問題及快速解決方法
下面小編就為大家?guī)硪黄狝ndroid 5.1 WebView內(nèi)存泄漏問題及快速解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
基于Android 監(jiān)聽ContentProvider 中數(shù)據(jù)變化的相關介紹
本篇文章小編為大家介紹,基于Android 監(jiān)聽ContentProvider 中數(shù)據(jù)變化的相關介紹。需要的朋友參考下2013-04-04

