如何在安卓中啟用 WebView 縮放控制元件?


以下示例演示如何在安卓中啟用 Webview 縮放控制元件。

第 1 步 − 在 Android Studio 中建立新專案,依次轉到“檔案 ⇒ 新專案”,然後填寫所有必需資訊以建立新專案。

第 2 步 − 將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <WebView
      android:id = "@+id/web_view"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
</LinearLayout>

在上述程式碼中,我們使用 WebView 展示 tutorialspoint.com。

第 3 步 − 將以下程式碼新增到 src/MainActivity.java

package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.P)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final ProgressDialog progressDialog = new ProgressDialog(this);
      progressDialog.setMessage("Loading Data...");
      progressDialog.setCancelable(false);
      WebView web_view = findViewById(R.id.web_view);
      web_view.requestFocus();
      web_view.getSettings().setLightTouchEnabled(true);
      web_view.getSettings().setBuiltInZoomControls(true);
      web_view.loadUrl("https://tutorialspoint.tw/");
      web_view.setWebChromeClient(new WebChromeClient() {
         public void onProgressChanged(WebView view, int progress) {
            if (progress < 100) {
               progressDialog.show();
            }
            if (progress = = 100) {
               progressDialog.dismiss();
            }
         }
      });
   }
}

第 4 步 − 將以下程式碼新增到 AndroidManifest.xml

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
   package = "com.example.myapplication">
   <uses-permission android:name = "android.permission.INTERNET"/>
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

我們來試執行你的應用程式。我假設你將實際的安卓移動裝置與你的計算機連線起來了。要從 Android Studio 執行應用程式,請開啟你的一個專案活動檔案,然後點選工具欄上的執行  圖示。選擇你的移動裝置作為選項,然後檢視顯示預設介面的移動裝置 –

點選 此處 下載專案程式碼

更新於: 30-7-2019

2 千+ 瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告