使用WebView组件浏览网页

 运行结果、结构目录:

        

 

 

 

activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <WebView
10         android:id="@+id/webView1"
11         android:layout_width="match_parent"
12         android:layout_height="match_parent" />
13 
14 
15 
16 </RelativeLayout>

 

MainActivity:
 1 package com.mingrisoft.myapplication;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.util.Log;
 6 import android.view.WindowManager;
 7 import android.webkit.WebChromeClient;
 8 import android.webkit.WebView;
 9 import android.webkit.WebViewClient;
10 
11 import java.net.URL;
12 
13 public class MainActivity extends AppCompatActivity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19 
20         //设置全屏显示
21         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
22                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
23         WebView webView = (WebView) findViewById(R.id.webView1);       //获取布局管理器中添加的WebView组件
24         webView.getSettings().setUseWideViewPort(true);    //设置此属性,可任意比例缩放
25         webView.getSettings().setLoadWithOverviewMode(true); //设置加载内容自适应屏幕
26         //使WebView组件具有放大和缩小网页的功能
27         webView.getSettings().setSupportZoom(true);
28         webView.getSettings().setBuiltInZoomControls(true);
29         webView.loadUrl("http://health.people.com.cn");    //指定要加载的网页
30     }
31 }

 

 

manifests:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     package="com.mingrisoft.myapplication">
 5     <uses-permission android:name="android.permission.INTERNET"/>
 6 
 7     <application
 8         android:usesCleartextTraffic="true"
 9         android:allowBackup="true"
10         android:icon="@mipmap/ic_launcher"
11         android:label="WebView组件浏览网页"
12         android:roundIcon="@mipmap/ic_launcher_round"
13         android:supportsRtl="true"
14         android:theme="@style/AppTheme"
15         tools:ignore="GoogleAppIndexingWarning">
16         <activity android:name=".MainActivity">
17             <intent-filter>
18                 <action android:name="android.intent.action.MAIN" />
19 
20                 <category android:name="android.intent.category.LAUNCHER" />
21             </intent-filter>
22         </activity>
23     </application>
24 
25 </manifest>

 

posted @ 2020-04-13 19:24  hemeiwolong  阅读(259)  评论(0编辑  收藏  举报