首页 > 系统 > Android > 正文

Android--webview遇到的坑

2019-11-09 16:35:08
字体:
来源:转载
供稿:网友

1.在webview中遇到html有输入框,而且需要全屏的时候

这时候即使html写了获取焦点也不会推动webview的内容,这里涉及到AndroidBug5497的问题,在某网大牛发出的代码在我项目里还是有点问题,于是我修改了一下,具体如下:public class AndroidBug5497Workaround { PRivate int mDownY; // 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 AndroidBug5497Workaround(activity); } private WebView mChildOfContent; private int usableHeightPrevious; private LinearLayout.LayoutParams frameLayoutParams; Activity mActivity; private AndroidBug5497Workaround(Activity activity) { mActivity = activity; //FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); LinearLayout content = (LinearLayout) mActivity.findViewById(R.id.rootview); //mChildOfContent = content.getChildAt(0); mChildOfContent = (WebView) content.getChildAt(0); mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { possiblyResizeChildOfContent(); } }); frameLayoutParams = (LinearLayout.LayoutParams) mChildOfContent.getLayoutParams(); //ViewGroup.LayoutParams layoutParams = mContent.getLayoutParams(); mChildOfContent.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()){ case MotionEvent.ACTION_DOWN: mDownY = (int)motionEvent.getY(); //Log.d("tag","---------------"+mDownX+" , "+ mDownY); break; } return false; } }); } 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; if (mDownY > usableHeightNow) { //如果点击点的高度比可用的高度还高,说明点击的位置被键盘挡住了 //将webview推到 以down点下面50px处,以这点为键盘的顶点 frameLayoutParams.topMargin = - (mDownY + 50 - usableHeightNow); } //Log.d("tag","+++++++++up height +++++++ "+heightDifference); } else { // keyboard probably just became hidden //frameLayoutParams.height = usableHeightSansKeyboard; frameLayoutParams.topMargin = 0; //Log.d("tag","+++++++++down height +++++++ "+frameLayoutParams.height); } //解决键盘缩小后的白屏的bug mChildOfContent.setLayerType(View.LAYER_TYPE_HARDWARE,null); //mChildOfContent.setLayoutParams(frameLayoutParams); mChildOfContent.requestLayout(); usableHeightPrevious = usableHeightNow; } } private int computeUsableHeight() { Rect r = new Rect(); //mChildOfContent.getWindowVisibleDisplayFrame(r); mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r); //无状态栏时top等于0 //Log.d("tag","++++++++++++++++ bottom "+r.bottom+" , top :" +r.top); return (r.bottom - r.top); }}

在oncreate方法里面添加一句:(setContentView下面)

AndroidBug5497Workaround.assistActivity(this);

2.在webview的html里面使用到iframe的时候

不能使用webview作为浏览器,只能使用内置的,不然会出现iframe部分白屏的现象,代码即: @Override public boolean shouldOverrideUrlLoading(WebView view, final String url) { return false; }

3.关于webview里面域的问题

在4.4之前是webkit内核,后成了Chrome,然后域引起的安全问题就来。网上大部分的解决方法都是放到服务器上,少用file://,但如果是不考虑安全问题的,可以直接强行跨域。 在4.4后有关于跨域的三个属性都默认为false,建议关闭。开启后即可跨域即:settings.setAllowContentaccess(true);settings.setAllowFileAccessFromFileURLs(true);settings.setAllowUniversalAccessFromFileURLs(true);如果高于4.4版本,则需要:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){ mWeb9996.getSettings().setAllowUniversalAccessFromFileURLs(true);}else{ try { Class<?> clazz = mWeb9996.getSettings().getClass(); Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class); if (method != null) { method.invoke(mWeb9996.getSettings(), true); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }}

最后说一句,不要怀疑自己,都是html的锅,恩就这样…


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表