Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發基礎控件:WebView 上手實戰

編輯:關於 Android 開發

Android 開發基礎控件:WebView 上手實戰

想在 App 裡直接打開一個網頁怎麼辦?Google 早就準備好了答案——WebView 控件。本文以一個帶標題欄的簡易瀏覽器為例,把它的用法完整走一遍。

一、界面結構拆解

整個界面上下兩塊:頂部是一條自製標題欄,由兩個 Button(返回、刷新)和一個 TextView(顯示網頁標題)組成;下方鋪滿一個 WebView。系統自帶的標題欄通過 AndroidManifest.xml 中設置對應主題去掉,就能得到上圖的效果。

二、布局代碼

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <Button
            android:id="@+id/quit"
            android:layout_gravity="left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回" />

        <TextView
            android:id="@+id/web"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="222dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.13" />

        <Button
            android:id="@+id/news"
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="刷新" />
    </LinearLayout>

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

三、MainActivity 邏輯

public class MainActivity extends Activity {

    private TextView mTextView;
    private WebView mWebView;
    private Button mbreak;
    private Button mnews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    public void init() {
        mTextView = (TextView) findViewById(R.id.web);
        mWebView = (WebView) findViewById(R.id.webView);
        mbreak = (Button) findViewById(R.id.quit);
        mnews = (Button) findViewById(R.id.news);

        mbreak.setOnClickListener(new myListener());
        mnews.setOnClickListener(new myListener());

        // 指定要加載的網址
        mWebView.loadUrl("https://www.baidu.com/");

        // 讓標題欄能拿到網頁的 title
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                mTextView.setText(title);
            }
        });

        // 頁面內跳轉不甩給系統瀏覽器
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    // 按鈕事件
    class myListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.quit:
                    finish();
                    break;
                case R.id.news:
                    mWebView.reload();
                    break;
            }
        }
    }
}

四、別忘了網絡權限

最後一步很多人會漏:在 AndroidManifest.xml 中加上網絡訪問聲明,否則頁面永遠白屏。

<uses-permission android:name="android.permission.INTERNET"/>

至此,一個能顯示網頁、能同步標題、能返回與刷新的簡易瀏覽器就完成了。WebView 的能力遠不止這些——前進後退歷史管理、JavaScript 交互、緩存策略、自定義 User-Agent 等都值得後續深入,這裡先把地基打好。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved