android 布局适配虚拟键适配
今天,看到關(guān)于虛擬鍵盤的內(nèi)容,于是記錄一下。
如果是控件,可以直接使用Android:fitsSystemWindows=”true”),但是如果是popwindow,那就必須獲取虛擬鍵盤的大小,來確定位置。
可以發(fā)現(xiàn),虛擬鍵位,擋住了取消按鈕的觸控區(qū)域,網(wǎng)上百度一下,大多是在布局內(nèi)家加上(Android:fitsSystemWindows=”true”) ,BUT我的控件不是布局,里面寫好的啊!我采用的自定義布局,SO 問題就是如何讓取消按鈕的觸控區(qū)域顯示出來。
解決辦法:獲取虛擬鍵高度,然后定位顯示布局的位置
public static Point getNavigationBarSize(Context context) {Point appUsableSize = getAppUsableScreenSize(context);Point realScreenSize = getRealScreenSize(context);// navigation bar on the rightif (appUsableSize.x < realScreenSize.x) {return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);}// navigation bar at the bottomif (appUsableSize.y < realScreenSize.y) {return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);}// navigation bar is not presentreturn new Point(); }public static Point getAppUsableScreenSize(Context context) {WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display display = windowManager.getDefaultDisplay();Point size = new Point();display.getSize(size);return size; }public static Point getRealScreenSize(Context context) {WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display display = windowManager.getDefaultDisplay();Point size = new Point();if (Build.VERSION.SDK_INT >= 17) {display.getRealSize(size);} else if (Build.VERSION.SDK_INT >= 14) {try {size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {} catch (NoSuchMethodException e) {}}return size; }
另外還有一種解決問題的方案。
完美解決方案:
解釋一下下面的代碼,就是監(jiān)聽某個視圖的變化,當可以看見的高度發(fā)生變化時,就對這個視圖重新布局,保證視圖不會被遮擋,也不會浪費屏幕空間。這一點尤其可用在像華為手機等可以隱藏和顯示虛擬鍵盤上導(dǎo)致屏幕變化的手機上。
- 首先添加工具類AndroidBug54971Workaround
然后在你需要解決這個問題的Activity的onCreate方法的setContentView(R.layout.content_frame);后面添加上
如果你看的懂代碼,你肯定知道assistActivity方法里放入的View是你 要調(diào)整高度的視圖。
android 布局適配虛擬鍵適配就講完了。
就這么簡單。
總結(jié)
以上是生活随笔為你收集整理的android 布局适配虚拟键适配的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 自定义checkBox的
- 下一篇: android 自定义MP4播放器