Android调用WebView

public class ZX_Activity extends BaseActivity implements IContract.IMainZXView {

@BindView(R.id.webview_id)
WebView webviewId;




@Override
public void success(ZX_Bean bean) {
   
   
   //时间转换
    long releaseTime = result.getReleaseTime();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    long releaseTime1 = result.getReleaseTime();
    String format = simpleDateFormat.format(releaseTime1);

    timeId.setText(format);


    String content = bean.getResult().getContent();
    if (content.contains("http:http")) {
        content = content.replaceAll("http:http", "http");
    }

    WebSettings settings = webviewId.getSettings();
    settings.setJavaScriptEnabled(true);
    webviewId.setWebViewClient(new ArticleWebViewClient());

   // webviewId.seth
    webviewId.loadDataWithBaseURL(null,content,"text/html","utf-8",null);



}

@Override
protected int getLayout() {
    return R.layout.activity_z_x;
}



@Override
protected ZXPresenter initPresenter() {
    return new ZXPresenter();
}

@Override
protected void initData() {
    Intent intent = getIntent();
    int infoId = intent.getIntExtra("infoId", 0);
    getPresenter().loadDate(infoId);
}

private static class ArticleWebViewClient extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        //重置webview中img标签的图片大小
        imgReset(view);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

//对图片进行重置大小,宽度就是手机屏幕宽度,高度根据宽度比便自动缩放
private static void imgReset(WebView webView) {
    webView.loadUrl("javascript:(function(){" + "var objs = document.getElementsByTagName('img'); " +
            "for(var i=0;i<objs.length;i++)  " + "{" + "var img = objs[i];   " +
            "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " + "}" + "})()");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值