要在网页中查看你的应用程序里面?你的、要在、应用程序、里面

2023-09-06 23:47:40 作者:天冷致病↘心冷致命

我需要建立一个Android应用程序,让您查看在我的应用程序的网页。我需要这不是在一个浏览器,但我的应用程序。 我找到了答案和一些选项时,页面加载。 我想我会尝试分享我发现这里的信息后,我测试当然.....

I needed to build an Android app that lets you view a web page inside my app. I needed this not to be in a browser but my app. I found the answer and some options for when page is loaded. I thought I'd try sharing the info I found here, after I tested of course.....

推荐答案

首先需要INTERNET权限添加到您的清单。

First need to add INTERNET permission to your manifest.

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

然后,使用web视图类来显示网页。首先,创建布局包含的WebView:

Then, use the WebView class to display a web page. First, create a layout that contains a webview:

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

在你的活动,(大概是的onCreate),通过使用您创建的布局初始化的WebView对象。下面是一个例子。     私人的WebView的WebView;

In your Activity, (probably the onCreate), initialize a WebView object by using the layout you created. An example is below. private WebView webview;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.somelayout);

    String url = "http://bigdaddyapp.com";

    webview = (WebView) findViewById(R.id.myWebView);
    //next line explained below
    webview.setWebViewClient(new MyWebViewClient(this));
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl(url);
}

如果你想具体的选项,像抓住了页面加载时,你需要一个内部WebViewClient类。例如,您可以使用onPageStarted(...)方法做一些事情,每当一个新的页面在您的WebView加载:

If you'd like specific options, like catching pages as they load, you need an inner WebViewClient class. For example, you can use the onPageStarted(...) method to do something whenever a new page is loaded in your webview:

 public class MyWebViewClient extends WebViewClient {

  public MyWebViewClient() {
     super();
     //start anything you need to
  }

  public void onPageStarted(WebView view, String url, Bitmap favicon) {
     //Do something to the urls, views, etc.
  }
 }