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>

 

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