您的位置:首页 > 移动开发

Android 简单自定义WebView

2015-11-06 11:31 393 查看
最近在一个项目里面用到了WebView控件,由于之前项目中使用到未总结,导致现在又得重新再写一遍。现在把它整理出来,方便以后查阅。

/**
* 自定义WebView
* @author LangK
*
*/
public class MyWebView extends RelativeLayout{
/**
* 上下文
*/
private Context mContext;
/**
* 浏览器
*/
private WebView webView;
/**
* 加载进度
*/
private ProgressBar progressBar;

public MyWebView(Context context) {
super(context);
this.mContext = context;
// TODO Auto-generated constructor stub
initView();
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.mContext = context;
initView();
}
public MyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.mContext = context;
initView();
}

private void initView() {
// TODO Auto-generated method stub
View view = LayoutInflater.from(mContext).inflate(R.layout.view_webview, this);
webView = (WebView) findViewById(R.id.view_webView);
progressBar = (ProgressBar) findViewById(R.id.view_webview_progress);
initWebViewSet();
}

/**
* 初始化WebView设置
*/
@SuppressLint("SetJavaScriptEnabled")
private void initWebViewSet() {
// 设置编码
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setTextZoom(70);
// 设置背景颜色 透明
webView.setBackgroundColor(Color.argb(0, 0, 0, 0));
// 设置可以支持缩放
webView.getSettings().setSupportZoom(true);
// 设置缓存模式
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
// //添加Javascript调用java对象
webView.getSettings().setJavaScriptEnabled(true);
// 设置出现缩放工具
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
// 扩大比例的缩放设置此属性,可任意比例缩放。
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBlockNetworkImage(false);
// 不启用硬件加速
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

// 自适应屏幕
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

// 重新WebView加载URL的方法
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = "http://www.baidu.com";
view.loadUrl(url);
return true;
}

public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(mContext, "网络错误", Toast.LENGTH_LONG).show();
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});

webView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
}
});
}

/**
* 获取WebView
* @return
*/
public WebView getWebView(){
return webView;
}
}


代码里面注释写的都比较清楚,就不多叙述了。下面贴一下用到的配置文件。

-XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/view_webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="2dp"
android:id="@+id/view_webview_progress"
android:max="100"
style="@android:style/Widget.ProgressBar.Horizontal"
android:progressDrawable="@drawable/style_progress"
/>
</RelativeLayout>
</LinearLayout>


-XML资源文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- 背景  gradient是渐变,corners定义的是圆角 -->
<item android:id="@android:id/background">
<shape>
<corners android:radius="10dp" />

<solid android:color="#AAAAAA" />
</shape>
</item>
<!-- 进度条 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="10dip" />

<solid android:color="#FDFF7A" />
</shape>
</clip>
</item>

</layer-list>


Just all!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: