1. 程式人生 > >Android 簡單自定義WebView

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!