androidstudio 開發 簡單 webview window

1 在AndroidManifest.xml文件增加 允許網絡訪問權限

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <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>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

 

2 在MainActivity.java 加入代碼

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    WebView mWebview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //步驟1. 定義Webview組件
        mWebview  = (WebView) findViewById(R.id.webView1);
        mWebview.getSettings().setDisplayZoomControls(false);//是否使用內置縮放機制
        mWebview.getSettings().setJavaScriptEnabled(true); //// 設置與Js交互的權限
        mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); // 設置允許JS彈窗
        mWebview.getSettings().setSupportZoom(true);// 是否支持變焦
        mWebview.getSettings().setBuiltInZoomControls(true);// 設置WebView是否應該使用其內置變焦機制,顯示放大縮小
        mWebview.getSettings().setUseWideViewPort(true);//是否開啓控件viewport。默認false,自適應;true時標籤中指定寬度值生效
        mWebview.getSettings().setLoadWithOverviewMode(true);
        //使用緩存,否則localstorage等無法使用
        mWebview.getSettings().setDomStorageEnabled(true);
        //方式1. 加載一個網頁:
        mWebview.loadUrl("baidu");
        //步驟3. 複寫shouldOverrideUrlLoading()方法,使得打開網頁時不調用系統瀏覽器, 而是在本WebView中顯示
        mWebview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mWebview.setInitialScale(100);// 初始化時縮放
        //步驟2. 選擇加載方式
    }
}

3 html文件

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

    <!--顯示網頁區域-->
    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp" />
</RelativeLayout>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章