Android如何解決虛擬按鍵欄遮擋問題
最近在公司的項目中 , 華為用戶反饋出了一個問題 , 華為手機底部有虛擬按鍵欄把應用的底部內容遮擋住了 , 現(xiàn)在已經(jīng)把這個問題解決了 , 記錄一下,給各位遇到相同問題的童鞋做一下參考.
這里的解決方案還是相對比較簡單的,首先判斷用戶的手機是否存在虛擬按鍵,若存在,那么就獲取虛擬按鍵的高度,然后再用代碼設置相同高度的TextView,這樣手機的虛擬按鍵就不會將底部的內容遮擋住了。
處理虛擬按鍵欄工具類:
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; }}
調用工具類方法 , 獲取虛擬按鍵高度:
//處理虛擬按鍵//判斷用戶手機機型是否有虛擬按鍵欄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); }
到這里就結束啦!
以上就是Android如何解決虛擬按鍵欄遮擋問題的詳細內容,更多關于Android 虛擬按鍵欄遮擋的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. 詳解Android studio 動態(tài)fragment的用法2. Android如何加載Base64編碼格式圖片3. 什么是python的自省4. 基于android studio的layout的xml文件的創(chuàng)建方式5. 解決Android studio xml界面無法預覽問題6. 編程語言PHP在Web開發(fā)領域的優(yōu)勢在哪?7. .Net Core使用Coravel實現(xiàn)任務調度的完整步驟8. Vuex localStorage的具體使用9. 圖文詳解vue中proto文件的函數(shù)調用10. Vue3狀態(tài)管理的使用詳解

網(wǎng)公網(wǎng)安備