將HTML網頁(test.html)放到Android的assets內


或者從ADT的檔案管理來新增一個網頁檔案(test.html)


activity_main.xml
--------------------------------------------------------

xmlns:tools="http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:paddingBottom= "@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight= "@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

android:id= "@+id/webView1"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />



MainActivity.java
--------------------------------------------------------

package com.example.test_app04;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);

final String str_url = "file:///android_asset/test.html" ; //本機網頁
//final String str_url = "http://www.google.com.tw"; //也可以是網址
final WebView cusBrowser=(WebView)findViewById(R.id.webView1 );

WebSettings web_Set = cusBrowser.getSettings();

//預設是限制JavaScript,所以如有需要必需先開啟。
web_Set.setJavaScri ptEnabled(true);

//啟用網頁縮放功能
web_Set.setSupportZoom( true);
web_Set.setBuiltInZoomControls( true);

//預設值會使用Android本常的瀏覽器打開連接,如果想繼續自己的瀏覽器,
//則可以使用下列的方法:
cusBrowser.setWebViewClient( new WebViewClient());

cusBrowser.loadUrl(str_url);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu. main, menu);
return true;
}

}

arrow
arrow
    全站熱搜

    keven 發表在 痞客邦 留言(0) 人氣()