Android WebView的使用(禁止超链接调用其他浏览器 设置滚动条 禁止横竖屏切换重新加载网页 )

WebView是android的一个组件,它的内核是基于开源WebKit引擎。如果我们对WebView进行一些美化、包装,可以非常轻松的开发出自己的浏览器。

下面是我写的一个例子:

java文件:

package com.dannyAndroid.webviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewDemo extends Activity {
	private WebView webview = null;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		webview = (WebView) findViewById(R.id.web);
		webview.getSettings().setJavaScriptEnabled(true);// 开启Javascript支持
		webview.getSettings().setLoadsImagesAutomatically(true);// 设置可以自动加载图片
		webview.setHorizontalScrollBarEnabled(true);//设置水平滚动条
		webview.setVerticalScrollBarEnabled(false);//设置竖直滚动条
		String url = "http://10.*.*.*:8090/webapp/";
		webview.loadUrl(url);
		//如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接。
        //需要给WebView添加一个事件监听对象(WebViewClient),并重写shouldOverrideUrlLoading方法      
		webview.setWebViewClient(new MyWebViewClient());

	}

	private class MyWebViewClient extends WebViewClient {
		
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}


}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.dannyAndroid.webviewdemo"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name=".WebViewDemo"
                  android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

*禁止超链接的时候调用其他浏览器

如果希望点击链接由自己处理,而不是新开Android的系统browser中响应该链接。需要给WebView添加一个事件监听对象(WebViewClient),并重写shouldOverrideUrlLoading方法  (详见代码)


*设置滚动条

  webview.setHorizontalScrollBarEnabled(true);//设置水平滚动条,true表示允许使用
webview.setVerticalScrollBarEnabled(false);//设置竖直滚动条  ,false表示禁止使用


*禁止横竖屏切换的时候,重新加载网页

需要在AndroidManifest.xml中添加android:configChanges="keyboardHidden|orientation|screenSize" (详见代码)



参考:

http://www.2cto.com/kf/201108/101518.html


http://blog.csdn.net/boyupeng/article/details/6212651


  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值