Android 弹框webview加载数据,并且设置webview的字体

 private void showServiceWeb(String content){
        View localView = LayoutInflater.from(context).inflate(R.layout.layout_dialog_service_content, null);
        Dialog dialog = utilsManage.dialog(this, localView, Gravity.CENTER, false);
        ImageView img=localView.findViewById(R.id.service_img_cancel);
        LinearLayout layout=localView.findViewById(R.id.service_layout);
        Button btn=localView.findViewById(R.id.service_btn_ok);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        initWebView(content,layout);
        dialog.show();
    }

private void initWebView(String content,LinearLayout layout){
        String url="https://www.hao123.com/";

        // 设置屏幕自适应。
//        webView=new WebView(context);
        //创建一个LayoutParams宽高设定为全屏
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        //创建WebView
//        if (webView != null) {
//            Log.e(TAG, "initWebView !null: ");
//            Toast.makeText(context, "webview不为空", Toast.LENGTH_SHORT).show();
//            return;
//        }
        WebView webView = new WebView(context);
        //设置WebView的宽高
        webView.setLayoutParams(layoutParams);
        //把webView添加到容器中
        layout.addView(webView);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
//        webView.getSettings().setDefaultFontSize(56);
       // webView.getSettings().setMinimumFontSize(40);//设置最小字体
        webView.getSettings().setTextZoom(300);//设置字体大小
        // 建议禁止缓存加载,以确保在攻击发生时可快速获取最新的滑动验证组件进行对抗。
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 设置不使用默认浏览器,而直接使用WebView组件加载页面。
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadDataWithBaseURL(null,content, "text/html",  "utf-8", null);//解决乱码问题

    }



可参考布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="30dp"
        android:orientation="vertical"
        android:background="@drawable/btn_white_background_small_rediu">
        <TextView
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户协议及隐私政策"
            android:textStyle="bold"
            android:textSize="@dimen/title_size"/>

        <TextView
            android:id="@+id/main_service_content"
            android:padding="20dp"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="感谢您信任并使用信达众商城!\n\n
我们非常重视您的个人信息和隐私保护,为了更好的保障你的个人权益,在您使用我们产品前,请您认真阅读
《用户协议》和《隐私政策》的全部内容,同意并接受全部条款后开始使用我们的产品和服务。\n\n
若选择不同意,将无法使用我们的产品和服务,并退出应用。"
            android:textSize="@dimen/content_size"/>

        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:padding="12dp"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="@color/grey"
                android:background="@drawable/btn_grey_line_left_rediu"
                android:text="不同意"/>
            <TextView
                android:padding="12dp"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="@color/blue"
                android:background="@drawable/btn_grey_line_right_rediu"
                android:text="同意"/>
        </LinearLayout>

    </LinearLayout>


</RelativeLayout>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值