事情是这样的.....本来在高高兴兴的敲代码,突然发现一个设置了bug,我得键盘怎么把我输入框挡住了?怀着白丝不得齐姐的心情,看了一下新添加的功能,沉浸式渲染.....哦...该死的设计,当即打开我的百度,直接梭哈,还别说,文章一堆堆。
如:让我修改AndroidManifest.xml给activity加上
android:windowSoftInputMode="adjustResize"或
android:windowSoftInputMode=”adjustPan”
要不就布局上加个滑动
不懂就查百度不做说明了哈,我得发完歇班了,
然而并无卵用,继续继续,搜搜一顿找,轮子一顿翻,找了一堆,一个有用的都没有,我灵机一动
灵感上来了,我可以求一下软键盘的高度,外层控件的高度,然后根据软键盘的高度把外层控件高度缩小,这不就上来了嘛.....
说了就干,,,,又是一顿找,,,,据说谷歌兄弟 并无软键盘高度的api提供,不过也没事,我用整体的高度 - 显示的高度高度也没问题,然后问题又来了,我获取不到
Rect r = new Rect();
r.bottom
获取就是20000 不知道为啥,感觉挺失败的又把路堵死了
又灵机一动,,,是不是沉浸式搞的鬼,这是我使用的沉浸式方法
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
然后想着花店心思在这上面,把沉浸式又重新缕了一下,真让人难受啊,沉浸式网上一堆坑啊!!!踩不过来了都快。
正在我要找ui看能不能放我一马时,突然看到有好心人提供的轮子
implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2'
沉浸式修改的轮子,我下载回来一用还真可以哎
ImmersionBar.with(ZjMainActivity.this).transparentStatusBar() .statusBarDarkFont(true) .navigationBarDarkIcon(true) .init();
我得沉浸式是透明,但是问题又来了,字体不变黑呢,全是白的
我又是开始思考,当然哈手上也没停就是翻资料,还别说找到了.....
又是一位救命的神仙啊!!!
工具类直接贴了
package com.zijingcloud.util; import android.app.Activity; import android.content.res.Resources; import android.graphics.Rect; import android.os.Build; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.widget.FrameLayout; import org.webrtc.ContextUtils; public class AndroidBugWorkaround { // For more information, see https://code.google.com/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 void assistActivity (Activity activity) { new AndroidBugWorkaround(activity); } private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; Activity activity; private AndroidBugWorkaround(Activity activity) { this.activity =activity; FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); mChildOfContent = content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { possiblyResizeChildOfContent(); } }); frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); } private void possiblyResizeChildOfContent() { int usableHeightNow = computeUsableHeight(); if (usableHeightNow != usableHeightPrevious) { int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); int heightDifference = usableHeightSansKeyboard - usableHeightNow; if (heightDifference > (usableHeightSansKeyboard/4)) { // keyboard probably just became visible frameLayoutParams.height = usableHeightSansKeyboard - heightDifference+ getStatusBarHeight(); } else { // keyboard probably just became hidden frameLayoutParams.height = usableHeightSansKeyboard-getNavigationBarHeight(); } mChildOfContent.setBottom(frameLayoutParams.height); mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); return (r.bottom - r.top); } public static int getStatusBarHeight(){ int result = 0; int resourceId = ContextUtils.getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = ContextUtils.getApplicationContext().getResources().getDimensionPixelSize(resourceId); } return result; } /** * 获取底部虚拟导航栏的高度 * @return */ public int getNavigationBarHeight() { int height = 0; //屏幕实际尺寸 DisplayMetrics dm = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getRealMetrics(dm); int phoneHeight = dm.heightPixels; if (isNavigationBarExist()) { Resources resources =activity.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { //获取NavigationBar的高度 height = resources.getDimensionPixelSize(resourceId); } } if (height > 0){ //处理全屏模式下,部分手机isNavigationBarExist()始终返回true,NavigationBar的高度 int diffValue = (DensityUtil.getScreenHeight(ContextUtils.getApplicationContext()) + height) - phoneHeight; //这里对三星特别处理,以为三星机会把状态栏高度算入真正显示高度, //不过也可以理解,毕竟我们是全屏模式 if(!"SAMSUNG".equalsIgnoreCase(Build.MANUFACTURER)){ diffValue+= getStatusBarHeight(); } if (diffValue > 0){ height -= diffValue; } } return height; } /** * 检测底部虚拟导航栏是否存在 *有很多教程写通过读取系统参数,无效,因为有些有参数但是不展示 * @return */ public boolean isNavigationBarExist(){ ViewGroup vp = (ViewGroup) activity.getWindow().getDecorView(); if (vp != null) { for (int i = 0; i < vp.getChildCount(); i++) { vp.getChildAt(i).getContext().getPackageName(); if (vp.getChildAt(i).getId() != View.NO_ID && "navigationBarBackground".equals(activity.getResources().getResourceEntryName(vp.getChildAt(i).getId()))) { return true; } } } return false; } } 毕竟我也是嫖得,就此结束这次bug之旅,记录一下。
差点忘了两位好人的链接: