Android 简单自定义WebView

最近在一个项目里面用到了WebView控件,由于之前项目中使用到未总结,导致现在又得重新再写一遍。现在把它整理出来,方便以后查阅。

    /**
     * 自定义WebView
     * @author LangK
     *
     */
    public class MyWebView extends RelativeLayout{
        /**
         * 上下文
         */
        private Context mContext;
        /**
         * 浏览器
         */
        private WebView webView;
        /**
         * 加载进度
         */
        private ProgressBar progressBar;

        public MyWebView(Context context) {
            super(context);
            this.mContext = context;
            // TODO Auto-generated constructor stub
            initView();
        }
        public MyWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            this.mContext = context;
            initView();
        }
        public MyWebView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
            this.mContext = context;
            initView();
        }

        private void initView() {
            // TODO Auto-generated method stub
            View view = LayoutInflater.from(mContext).inflate(R.layout.view_webview, this);
            webView = (WebView) findViewById(R.id.view_webView);
            progressBar = (ProgressBar) findViewById(R.id.view_webview_progress);
            initWebViewSet();
        }


    /**
     * 初始化WebView设置
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void initWebViewSet() {
        // 设置编码
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.getSettings().setTextZoom(70);
        // 设置背景颜色 透明
        webView.setBackgroundColor(Color.argb(0, 0, 0, 0));
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        // 设置缓存模式
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // //添加Javascript调用java对象
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        // 扩大比例的缩放设置此属性,可任意比例缩放。
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBlockNetworkImage(false);
        // 不启用硬件加速
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        // 自适应屏幕
        webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

        // 重新WebView加载URL的方法
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                url = "http://www.baidu.com";
                view.loadUrl(url);
                return true;
            }

            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                Toast.makeText(mContext, "网络错误", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                progressBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }
        });

        webView.setWebChromeClient(new WebChromeClient(){
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
            }
        });
    }

        /**
         * 获取WebView
         * @return
         */
        public WebView getWebView(){
            return webView;
        }
    }

代码里面注释写的都比较清楚,就不多叙述了。下面贴一下用到的配置文件。
-XML布局文件

<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <WebView
                android:id="@+id/view_webView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
            <ProgressBar 
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:id="@+id/view_webview_progress"
                android:max="100"
             style="@android:style/Widget.ProgressBar.Horizontal"
                android:progressDrawable="@drawable/style_progress"
                />
               </RelativeLayout>
               </LinearLayout>

-XML资源文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 背景  gradient是渐变,corners定义的是圆角 -->
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="10dp" />

            <solid android:color="#AAAAAA" />
        </shape>
    </item>
    <!-- 进度条 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dip" />

                <solid android:color="#FDFF7A" />
            </shape>
        </clip>
    </item>

</layer-list>

Just all!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值