Android学习之WebView文档翻译

接着翻译,今天翻译WebView控件的使用,作为对前面博客的一个补充,相信会有更深的了解。

翻译文章来源:

http://guides.codepath.com/android/Working-with-the-WebView

Webview工作

概述:

如果你想提供一个网页应用程序(或者仅仅是一个网页)作为应用程序的一部分,你可以用WebView来实现它。它是android视图的一个延伸并且允许你展示网页作为你活动布局的一部分。

这个文档想你展示如何使用WebView和做一些附加的事情,例如用代码处理你网页的适配和js交互在android应用程序中。

If you want to deliver a web application (or just a web page) as a part of a client application, you can do it using WebView. The WebView class is an extension of Android’s View class that allows you to display web pages as a part of your activity layout.

This document shows you how to get started with WebView and how to do some additional things, such as handle page navigation and bind JavaScript from your web page to client-side code in your Android application. See the official WebView docs for a more detailed look.

加载外部页面

要得到网络允许,需要在清单文件中添加网络权限,如下所示:

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

添加一个WebView到你的应用程序,例如包含标签在你的布局中

To add a WebView to your Application, simply include the element in your activity layout:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

首先,我们需要设置webview合理的默认值来用于 WebSettings和WebViewClient

First, we need to configure the WebView to behave with reasonable defaults using WebSettings and creating a WebViewClient:

public class MainActivity extends Activity {
   private WebView myWebView;

   @Override        
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myWebView = (WebView) findViewById(R.id.webview);
      // Configure related browser settings
      //配置浏览器设置
      myWebView.getSettings().setLoadsImagesAutomatically(true);
      myWebView.getSettings().setJavaScriptEnabled(true);
      myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
      // Configure the client to use when opening URLs
      //当打开URL时配置客户端
      myWebView.setWebViewClient(new MyBrowser());
      // Load the initial URL
      //加载初始化URL
      myWebView.loadUrl("http://www.example.com");
   }

   // Manages the behavior when URLs are loaded
   //管理行为当URl被加载时
   private class MyBrowser extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }
}

处理反应布局(适配)

通常,webview不会计算默认的缩放比例如果网页包含viewport 数据,如果你希望在页面加载响应布局,你需要明确设置它。

By default, the WebView does not account for the default scale size if HTML pages include viewport metadata. If you wish to enable the page to load with responsive layouts, you need to set it explicitly:

// Enable responsive layout
myWebView.getSettings().setUseWideViewPort(true);
// Zoom out if the content width is greater than the width of the veiwport
myWebView.getSettings().setLoadWithOverviewMode(true);

你也可以对页面进行缩放

You can also enable the ability to zoom-in controls on the page:

myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setBuiltInZoomControls(true); // allow pinch to zooom
myWebView.getSettings().setDisplayZoomControls(false); // disable the default zoom controls on the page

加载本地页面

一般你想用Webview加载本地网页,你可以将本地网页放在Android程序的assets文件中,如果你没有在你的main文件夹下找到assets文件夹,这时你可以创建一个。将html,css,js,etc文件放在这个文件夹中

例如,我想加载本地页面index.html,我将创建我的文件在{ProjectName}/app/src/main/assets/index.html 下面,然后你可以在你的活动或者碎片中作如下设置

In case you want to store a copy of a webpage locally to be loaded into a WebView , you can put it in the android assets folder. If you do not find one under your main/ directory, then you can create one. Place the html, css, js, etc in this folder.

For example, say I wanted to load a page entitled index.html . I would create my file under {ProjectName}/app/src/main/assets/index.html

then, in your activity or fragment you can use the code

public class MainActivity extends Activity {
   private WebView myWebView;

   @Override        
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myWebView = (WebView) findViewById(R.id.webview);
      myWebView.getSettings().setJavaScriptEnabled(true);
      myWebView.setWebViewClient(new WebViewClient());
      String path = Uri.parse("file:///android_asset/index.html").toString();
      myWebView.loadUrl(path);
   }

}

关键在( String path = Uri.parse(“file:///android_asset/index.html”).toString();)
读取本地网页路径

共享缓存在webview和网络客户端之间

正确的使用webview自己的缓存管理器,这就意味着对于所有的网络请求,你都可以在外部分开保存它,这也会导致问题,当你缓存同样的文件时,最简单的方法就是堆栈溢出,在实现cookie处理当所有的请求都是从webview缓存获取时。

WebViews currently use their own cookie manager, which means that any network requests you make outside of these web views are usually stored separately. This can cause problems when trying to retain the same cookies (i.e. for authentication or cross-site script forgery (CSRF) headers). The simplest approach as proposed in this Stack Overflow article is to implement a cookie handler that forwards all requests to the WebView cookie store. See this gist for an example.

引用:

http://developer.android.com/guide/webapps/webview.html
http://developer.android.com/guide/webapps/best-practices.html
http://www.javacodegeeks.com/2013/06/fragment-in-android-tutorial-with-example-using-webview.html
http://www.tutorialspoint.com/android/android_webview_layout.htm
http://www.mkyong.com/android/android-webview-example/

翻译结束,主要介绍了webview的适配,缩放,加载本地网页,已经缓存。算是对前两篇博客的一个总结,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值