安卓中的WebView

3

如何在安卓中设置WebView?


看看我的教程吧。 http://www.taiic.com/2011/03/12/webview-template-for-android-sdk/ 我也提供了源代码。 - GeneK
3个回答


0
 **main.xml (res/layout)**
 <?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" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/urlEdit"
        android:layout_width="300dip"
        android:layout_height="wrap_content"
        android:inputType="textWebEditText"
        android:imeOptions="actionDone" />

    <Button
        android:id="@+id/goButton"
        android:layout_width="50dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_toRightOf="@id/urlEdit"
        android:background="@drawable/redbutton"
        android:text="Go..."
        android:textColor="#fcfcfc" />
  </RelativeLayout>

   <WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

 </LinearLayout>

WebView 活动

   public class WebViewExampleActivity extends Activity  {
   public WebView webView;
   public Button goButton;
   public EditText urlEdit;
   @Override
   public void onCreate(Bundle savedInstanceState)   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webView = (WebView)findViewById(R.id.webView);
    goButton = (Button)findViewById(R.id.goButton);
    urlEdit = (EditText)findViewById(R.id.urlEdit); 

    webView.setWebViewClient(new DemoWebViewClient());
    goButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            openUrl();
        }
    });
     }

     public void openUrl()    {
    String url;
    url = urlEdit.getText().toString();
    webView.loadUrl(url);
     }
     public class DemoWebViewClient extends WebViewClient     {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    }
    }

0

这是它们

xml

<WebView
  android:id="@+id/web_view"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"      
  android:layout_weight="1.0" />

    WebView webview = (WebView) findViewById(R.id.web_view);
    webview.getSettings().setSupportZoom(true);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.loadUrl("file:///android_asset/yourHtmlFile.html");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接