Android WebView 中遇到的问题集锦

1、解决webview 键盘弹起,布局被遮挡的问题(转载) 填坑 android系统 5497BUG


 //卸载初始化里面,解决
  WebViewBugUtil.assistActivity(mActivity);

/**
 * Data: 2019/6/11
 * Description:
 * 解决webview 键盘弹起,布局被遮挡的问题(转载)
 *  填坑 android系统 5497BUG  AndroidBug5497Workaround
 *  参考资料:http://blog.csdn.net/u010897392/article/details/53171071
 * @author: zhanggf
 */
public class WebViewBugUtil {
    public static void assistActivity(Activity activity) {
        new WebViewBugUtil(activity);
    }
    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
    private int contentHeight;
    private   boolean isfirst = true;
    private   Activity activity;
    private  int statusBarHeight;

    private WebViewBugUtil(Activity activity) {
        //获取状态栏的高度
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
        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() {
                if (isfirst) {
                    contentHeight = mChildOfContent.getHeight();//兼容华为等机型
                    isfirst = false;
                }
                possiblyResizeChildOfContent();
            }
        });

        frameLayoutParams = (FrameLayout.LayoutParams)
                mChildOfContent.getLayoutParams();
    }

    //重新调整跟布局的高度
    private void possiblyResizeChildOfContent() {

        int usableHeightNow = computeUsableHeight();

        //当前可见高度和上一次可见高度不一致 布局变动
        if (usableHeightNow != usableHeightPrevious) {
            //int usableHeightSansKeyboard2 = mChildOfContent.getHeight();//兼容华为等机型
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                // keyboard probably just became visible
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                    //frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;
                } else {
                    frameLayoutParams.height = usableHeightSansKeyboard -heightDifference;
                }
            } else {
                frameLayoutParams.height = contentHeight;
            }

            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }
    /**     * 计算mChildOfContent可见高度     ** @return     */
    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}



2、关于WebView滑动卡顿、加载白屏、内部跳转卡死无效等

整体修改的两种方式,项目中有遇到不流畅,偶现第一次进入白屏,添加了如下加速解决

		//只添加软件加速,测试可能会导致滑动卡顿且会弹得问题
   	   if (Build.VERSION.SDK_INT >= 19) {//硬件加速器的使用
			webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		} else {
			webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}

但是后期又遇到了频繁进入加载白屏,且H5内部跳转无法跳转或卡死现象,经过百度查找测试通过如下方式解决,—父布局添加硬件加速,webView添加软件加速,啥原因有知道的欢迎赐教

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/parent_bg"
    android:layerType="hardware"
    android:orientation="vertical">


    <wendu.webviewjavascriptbridge.WVJBWebView
        android:id="@+id/wb_main"
        android:layerType="software"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

3、顺便附上在WebView中一些设置以及遇到得问题


		//基础设置
		 WebSettings webSettings = webView.getSettings();
        webSettings.setTextZoom(100);//设置字体缩放比例
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);// 适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//适应内容大小
  	  webSettings.setLoadsImagesAutomatically(true);  //支持自动加载图片

		// 启动webview的html5的本地存储功能。
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = activity.getApplicationContext().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        
  		//webview加载https页面图片不能显示问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

		 //只在debuggable为true时候允许WebView远程调试
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (0 != (activity.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
                WebView.setWebContentsDebuggingEnabled(true);
            }
        }

		//找不到网页net:err_unknown_url_scheme:一般情况是由于协议不同引起的,添加下面的设置,如果不		       是HTTP或HTTPS协议则由浏览器进行解读
        webSettings.setUserAgentString("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0");


		

3、关于友盟统计: Fragment中的页面访问路径怎么统计不到? 页面访问路径无数据? debug报如下错误

在这里插入图片描述
官方描述:https://developer.umeng.com/docs/66632/detail/66975?um_channel=sdk

看完文档,表示“需要保证线性不交叉”这个就懵逼了,怎么能保证线性不交叉,一般都直接写在BaseFragment里面,

而在首页tab切换,就有问题了。
想过去掉ViewPager预加载功能,没测试,有需要可以试试,用了一种比较粗暴的方式解决了

解决方式:

再Activity里面切换时强制配对onPageEnd结束上一个,再onPageStart开始新的Fragment

 private String mPageName;
 //初始化默认赋值:        
  mPageName = "MineFragment";

  case R.id.tv_home:
                switchTab(view.getId(), HOMEPAGE_TAG);
                MobclickAgent.onEvent(this, UmengConstants.main_tab_home);
                MobclickAgent.onPageEnd(mPageName);
                mPageName = "HomeIndexFragment";
                MobclickAgent.onPageStart(mPageName);
                break;
            case R.id.tv_msg: //消息
                switchTab(view.getId(), MSGPAGE_TAG);
                MobclickAgent.onEvent(this, UmengConstants.main_tab_session);
                MobclickAgent.onPageEnd(mPageName);
                mPageName = "SessionListFragment";
                MobclickAgent.onPageStart(mPageName);
                break;
            case R.id.tv_find: //发现
                switchTab(view.getId(), FINDPAGE_TAG);
                MobclickAgent.onEvent(this, UmengConstants.main_tab_find);
                MobclickAgent.onPageEnd(mPageName);
                mPageName = "OnlineFragment";
                MobclickAgent.onPageStart(mPageName);
                break;
            case R.id.tv_me:
                switchTab(view.getId(), MINEPAGE_TAG);
                MobclickAgent.onEvent(this, UmengConstants.main_tab_mine);
                MobclickAgent.onPageEnd(mPageName);
                mPageName = "MineFragment";
                MobclickAgent.onPageStart(mPageName);
                break;


官方文档写在前面:
BaseActivity

  // Activity页面onResume函数重载
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this); // 不能遗漏

    }

    // Activity页面onResume函数重载
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this); // 不能遗漏
    }

BaseFragment:
 // Fragment页面onResume函数重载
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面("MainScreen"为页面名称,可自定义)

    }

    // Fragment页面onResume函数重载
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(mPageName);
    }

   @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(mPageName);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值