Android 简单自定义WebView
2015-11-06 11:31
393 查看
最近在一个项目里面用到了WebView控件,由于之前项目中使用到未总结,导致现在又得重新再写一遍。现在把它整理出来,方便以后查阅。
代码里面注释写的都比较清楚,就不多叙述了。下面贴一下用到的配置文件。
-XML布局文件
-XML资源文件
Just all!
/** * 自定义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!
相关文章推荐
- iPhone/Mac Objective-C内存管理教程和原理剖析
- IOS日期转为今天昨天形式
- APP 完整性校验
- iOS中html5的交互:WebViewJavascriptBridge
- android自定义带返回功能的标题
- Android中画廊视图Gallery和ImageSwitcher组件的使用(十三)
- iOS异常处理
- cocosJs 学习文章 地址
- Android SIM内ECC文件存储超过5个就无法匹配service category
- android studio 学习-如何打包apk
- Android FDN号码完全匹配
- Objective-C对象模型及应用
- Android Studio中 Android butterKnife Zelezny和 Android Parcelable code generator的使用(备忘录)
- swift优秀学习博客
- Android FDN开启后手机仍然能够上网
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- Android 日历插件
- 【FastDev4Android框架开发】列表头生成带文本或者字母的图片开源库TextDrawable使用和详解(二十二)
- 对Objective-C中Block的追探
- AS3.0杂记——Dictionary、Object与Array