开发者

Load URL Within App

开发者 https://www.devze.com 2023-04-13 02:21 出处:网络
I am looking for a way to load an external website within a page in the app. For example have a website link for a company on the main 开发者_如何转开发screen and when clicked have the transition eff

I am looking for a way to load an external website within a page in the app.

For example have a website link for a company on the main 开发者_如何转开发screen and when clicked have the transition effect to another page that displays the company website, but also allows you to include the header and footer for the page. You can also include options to go back, or other links.

I believe this is possible, so any help with this would be great. Thanks,


I believe you are talking about Android's WebView class. http://developer.android.com/reference/android/webkit/WebView.html

A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.


http://developer.android.com/resources/tutorials/views/hello-webview.html

You just need to use the webview view. After that it's pretty simple to place the view wherever you want and send it to any url you'd like.

WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://www.google.com");
}


I currently am using this method and it works.

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ThirdActivity extends Activity
{
    WebView webview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.html_view);
        webview = (WebView)findViewById(R.id.viewHTML);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("https://twitter.com/LaserPros");
    }
}

don't forget your xml file mine is named html_view it has a webview in it with the id of viewHTML the file looks like this:

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

in the class activity the important thing to (and what most people miss when giving an answer) is to set your web view as the WebClient otherwise your phone will want to open the url in a external browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号