国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android--webview遇到的坑

2019-11-09 17:37:55
字體:
供稿:網(wǎng)友

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

這時候即使html寫了獲取焦點也不會推動webview的內(nèi)容,這里涉及到AndroidBug5497的問題,在某網(wǎng)大牛發(fā)出的代碼在我項目里還是有點問題,于是我修改了一下,具體如下: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); //無狀態(tài)欄時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作為瀏覽器,只能使用內(nèi)置的,不然會出現(xiàn)iframe部分白屏的現(xiàn)象,代碼即: @Override public boolean shouldOverrideUrlLoading(WebView view, final String url) { return false; }

3.關(guān)于webview里面域的問題

在4.4之前是webkit內(nèi)核,后成了Chrome,然后域引起的安全問題就來。網(wǎng)上大部分的解決方法都是放到服務(wù)器上,少用file://,但如果是不考慮安全問題的,可以直接強行跨域。 在4.4后有關(guān)于跨域的三個屬性都默認為false,建議關(guān)閉。開啟后即可跨域即: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的鍋,恩就這樣…


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邢台市| 内乡县| 虹口区| 盘锦市| 邳州市| 惠水县| 元阳县| 岳西县| 惠来县| 大同市| 丰城市| 门头沟区| 张家港市| 江门市| 农安县| 宜兰市| 永登县| 璧山县| 永城市| 错那县| 厦门市| 泽州县| 博客| 开封市| 英吉沙县| 兰西县| 望城县| 九龙坡区| 花莲市| 绍兴县| 淮安市| 连云港市| 淄博市| 宜兴市| 施甸县| 岱山县| 新乡县| 边坝县| 香港 | 师宗县| 临武县|