1.空布局
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.administrator.webview.MainActivity"> 11 12 13 </RelativeLayout>
2. MainAtivity
1 package com.example.administrator.webview; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.KeyEvent; 6 import android.webkit.WebView; 7 8 public class MainActivity extends AppCompatActivity { 9 10 WebView webview; 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 webview = new WebView(this); 16 //设置WebView属性,能够执行Javascript脚本 17 webview.getSettings().setJavaScriptEnabled(true); 18 //加载需要显示的网页 19 webview.loadUrl("http://web.breadtrip.com/"); 20 //设置Web视图 21 setContentView(webview); 22 } 23 @Override 24 25 public boolean onKeyDown(int keyCode, KeyEvent event) { 26 if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 27 webview.goBack(); //goBack()表示返回WebView的上一页面 28 return true; 29 } 30 return false; 31 } 32 }
----------------------
1. 布局
1 <WebView 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 android:id="@+id/chart2" 5 android:background="#000000" 6 android:visibility="visible" 7 />
2.Activity
1 2 webview3 = (WebView) findViewById(R.id.chart3); 3 4 5 //设置WebView属性,能够执行Javascript脚本 6 7 webview3.getSettings().setJavaScriptEnabled(true); 8 //加载需要显示的网页 9 10 webview3.loadUrl(url2);