1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 解决输入法遮挡布局和华为手机底部虚拟按键遮挡底部界面的方法

解决输入法遮挡布局和华为手机底部虚拟按键遮挡底部界面的方法

时间:2021-02-03 23:29:22

相关推荐

解决输入法遮挡布局和华为手机底部虚拟按键遮挡底部界面的方法

先上最终的代码:

public classAndroidWorkaround {public static voidassistActivity(View content) {newAndroidWorkaround(content);}privateViewmChildOfContent;private intusableHeightPrevious;privateViewGroup.LayoutParamsframeLayoutParams;privateAndroidWorkaround(View content) {mChildOfContent= content;mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {public voidonGlobalLayout() {possiblyResizeChildOfContent();}});frameLayoutParams=mChildOfContent.getLayoutParams();}private voidpossiblyResizeChildOfContent() {intusableHeightNow = computeUsableHeight();if(usableHeightNow !=usableHeightPrevious) {frameLayoutParams.height= usableHeightNow;mChildOfContent.requestLayout();usableHeightPrevious= usableHeightNow;}}private intcomputeUsableHeight() {Rect r =newRect();mChildOfContent.getWindowVisibleDisplayFrame(r);return(r.bottom);}public static booleancheckDeviceHasNavigationBar(Context context) {booleanhasNavigationBar =false;Resources rs = context.getResources();intid = rs.getIdentifier("config_showNavigationBar","bool","android");if(id > 0) {hasNavigationBar = rs.getBoolean(id);}try{Class systemPropertiesClass = Class.forName("android.os.SystemProperties");Method m = systemPropertiesClass.getMethod("get", String.class);String navBarOverride = (String) m.invoke(systemPropertiesClass,"qemu.hw.mainkeys");if("1".equals(navBarOverride)) {hasNavigationBar =false;}else if("0".equals(navBarOverride)) {hasNavigationBar =true;}}catch(Exception e) {}returnhasNavigationBar;}}

注意点:这里构造方法的参数是当前你写的布局view

-----------------------------------------------------------------------------------------------

PS:

1.单单解决输入法挡住软键盘

public classAndroidBug5497Workaround {// For more information, see /p/android/issues/detail?id=5497 // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.public static voidassistActivity(Activity activity) {newAndroidBug5497Workaround(activity);}privateActivityactivity;privateViewmChildOfContent;private intusableHeightPrevious;privateFrameLayout.LayoutParamsframeLayoutParams;privateAndroidBug5497Workaround(Activity activity) {this.activity= activity;FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);mChildOfContent= content.getChildAt(0);mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {public voidonGlobalLayout() {possiblyResizeChildOfContent();}});frameLayoutParams= (FrameLayout.LayoutParams)mChildOfContent.getLayoutParams();}private voidpossiblyResizeChildOfContent() {intusableHeightNow = computeUsableHeight();if(usableHeightNow !=usableHeightPrevious) {intusableHeightSansKeyboard =mChildOfContent.getRootView().getHeight();//这个判断是为了解决19之前的版本不支持沉浸式状态栏导致布局显示不完全的问题if(Build.VERSION.SDK_INT< Build.VERSION_CODES.KITKAT) {Rect frame =newRect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);intstatusBarHeight = frame.top;usableHeightSansKeyboard -= statusBarHeight;}intheightDifference = usableHeightSansKeyboard - usableHeightNow;if(heightDifference > (usableHeightSansKeyboard / 4)) {// keyboard probably just became visibleframeLayoutParams.height= usableHeightSansKeyboard - heightDifference;}else{// keyboard probably just became hiddenframeLayoutParams.height= usableHeightSansKeyboard;}mChildOfContent.requestLayout();usableHeightPrevious= usableHeightNow;}}private intcomputeUsableHeight() {Rect frame =newRect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);intstatusBarHeight = frame.top;Rect r =newRect();mChildOfContent.getWindowVisibleDisplayFrame(r);//这个判断是为了解决19之后的版本在弹出软键盘时,键盘和推上去的布局(adjustResize)之间有黑色区域的问题if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {return(r.bottom- r.top) + statusBarHeight;}return(r.bottom- r.top);}}

2.单单解决华为手机虚拟按键挡住底部界面

public classAndroidBug54971Workaround {// For more information, see /p/android/issues/detail?id=5497 // To use this class, simply invoke assistActivity() on an Activity that already has its content view set. /** * 关联要监听的视图 * *@paramviewObserving*/public static voidassistActivity(View viewObserving) {newAndroidBug54971Workaround(viewObserving);}privateViewmViewObserved;//被监听的视图private intusableHeightPrevious;//视图变化前的可用高度privateViewGroup.LayoutParamsframeLayoutParams;privateAndroidBug54971Workaround(View viewObserving) {mViewObserved= viewObserving;//给View添加全局的布局监听器mViewObserved.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {public voidonGlobalLayout() {resetLayoutByUsableHeight(computeUsableHeight());}});frameLayoutParams=mViewObserved.getLayoutParams();}private voidresetLayoutByUsableHeight(intusableHeightNow) {//比较布局变化前后的View的可用高度if(usableHeightNow !=usableHeightPrevious) {//如果两次高度不一致 //将当前的View的可用高度设置成View的实际高度frameLayoutParams.height= usableHeightNow;mViewObserved.requestLayout();//请求重新布局usableHeightPrevious= usableHeightNow;}}/** * 计算视图可视高度 * *@return*/private intcomputeUsableHeight() {Rect r =newRect();mViewObserved.getWindowVisibleDisplayFrame(r);return(r.bottom- r.top);}}

注意点:setContentView(R.layout.content_frame);

AndroidBug54971Workaround.assistActivity(findViewById(android.R.id.content));

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。