Android Web视图布局示例
在之前的教程中,我们已经学习了Android中的线性和相对布局,本教程用示例来解释web视图布局。
我们想在Android应用程序中打开网页吗?Android提供了一个叫做web视图的布局,它可以帮助我们在应用程序中打开web页面。
Web视图布局
Web视图布局用于显示活动中的联机内容。Web视图类是Android视图类的扩展。它不像web浏览器,所以它不提供导航控制和URL。webkit呈现引擎显示网页并允许我们向前和向后导航。如果你想在你的应用程序中显示一些网页信息,Web视图是非常常用的。或者另一个用途是,如果我们想提供需要更新的信息,如用户教程或者协议。
如果要在应用程序中访问internet,则必须将internet权限添加到列表文件中,例如
<uses-permission android: name= "android.permission.INTERNET"
网页浏览方式
以下是Android中web视图布局的方法。
addJavascriptInterface(Object Object,String name),用于在web视图布局中注入JavaScript代码。
canGoBack(),用于转到上一历史项。
canGoBackOrForward(int steps),用于按给定的步骤后退或者前进。
canGoForward(),用于转到下一个历史项。
canZoonIn(),用于放大。
canZoomOut(),用于缩小。
clearHistory(),用于清除web视图的历史记录。
clearView(),用于重置web视图并释放资源。
destroy(),用于破坏web视图的内部状态。
findFocus()在当前具有根视图的层次结构中查找。
freemory(),用于释放内存,现在不推荐使用此方法。
getSettings(),获取web视图的设置。
getTitle(),以字符串形式获取当前页面的标题。
getURL(),以字符串形式获取当前页面的URL。
goBack(),回到网页浏览的历史。
capturePicture(),用于对网络视图进行位图快照。
Android中的Web视图示例
下面是一个示例,演示如何显示theitroad.本地通过在活动中使用web视图布局在应用程序中创建。首先你需要创建一个新的活动。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:text="Welcome to theitroad.local"
android:textColor="@android:color/black"
android:textSize="24sp"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="Click Here"
android:id="@+id/button"
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:layout_marginTop="86dp"
android:id="@+id/webView"
</RelativeLayout>
mainActivity.java
package com.example.admin.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button b1;
private WebView wv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.theitroad.local/";
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
});
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
你的manifest文件应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.webview">
<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>

