webview
次のコードは、Android 2.2 の 854x480 ピクセルの画面に収まるように完全に縮小された Google ホームページのデスクトップ バージョンを読み込みます。デバイスの向きを変えて、縦向きまたは横向きでリロードすると、毎回ページ幅がビュー内に完全に収まります。
BrowserLayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Browser.java:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Browser extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.BrowserLayout);
String loadUrl = "http://www.google.com/webhp?hl=en&output=html";
// initialize the browser object
WebView browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
try {
// load the url
browser.loadUrl(loadUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
}