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 虛擬按鍵欄遮擋的資料請關注腳本之家其它相關文章!
相關文章
Android ApplicationInfo 應用程序信息的詳解
這篇文章主要介紹了Android ApplicationInfo 應用程序信息的詳解的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10Android?Studio實現(xiàn)購買售賣系統(tǒng)
這篇文章主要為大家詳細介紹了Android?Studio實現(xiàn)購買售賣系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Android客制化adb shell進去后顯示shell@xxx的標識
今天小編就為大家分享一篇關于Android客制化adb shell進去后顯示shell@xxx的標識,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Android 使用Vibrator服務實現(xiàn)點擊按鈕帶有震動效果
這篇文章主要介紹了Android 使用Vibrator服務實現(xiàn)點擊按鈕帶有震動效果,,本文通過實例圖文相結合給大家介紹的非常詳細,對大家的學習火鍋工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Flutter?Widget之NavigationBar使用詳解
這篇文章主要為大家介紹了Flutter?Widget之NavigationBar使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12