android > 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/wv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

主activity

package t4.mft;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class T4Activity extends Activity {
    /** Called when the activity is first created. */
	WebView wv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        wv = (WebView)findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setScrollBarStyle(0);
        wv.loadUrl("file:///android_asset/sample.html");
        
        //在webView 加载完后 执行js
        wv.setWebViewClient(new WebViewClient()
        {   
        	@Override
        	public void onPageFinished(WebView view, String url){
        		super.onPageFinished(view, url);
        		wv.loadUrl("javascript:wave()");
        	}
        });
        
        
    }
}

判断 一个 webview 是否加载完成的方法

mWebView.setWebViewClient(new WebViewClient()
{   
                        @Override
                        public void onPageFinished(WebView view, String url) 
                        {

                                //开始
                                super.onPageFinished(view, url);
                        }
                        @Override
                        public void onPageStarted(WebView view, String url, Bitmap favicon) 
                        {

                                //结束
                                super.onPageStarted(view, url, favicon);

                        }
});

在webview 加载中 设置背景颜色

 webView.setBackgroundColor(Color.parseColor("#000000"));

设置滚动条不显示

	    wv_yali.setHorizontalScrollBarEnabled(false);//水平不显示
	    wv_yali.setVerticalScrollBarEnabled(false); //垂直不显示
wb.setInitialScale(95);	//缩放比例
/// 得到焦点后不放大
wb.getSettings().setSupportZoom( true ); //Modify this
wb.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);//Add this
//\\\

让输入的键盘类型 为 数字 或其他

<inputtype="number"/><inputtype="tel"/>

<input type="search" />

 **不弹出 默认浏览器在 WebView 中加载

WebView wv;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    wv = (WebView) findViewById(R.id.wv1);
    wv.loadUrl("http://wangjun.easymorse.com/");
    wv.setWebViewClient(new WebViewClientDemo());

}

private class WebViewClientDemo extends WebViewClient {
    @Override
    // 在WebView中而不是默认浏览器中显示页面
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

猜你喜欢

转载自mft.iteye.com/blog/1676057
今日推荐