当我们使用浏览器浏览网页时,总会看到下图页面的样子,上面是一个地址栏,地址栏下面显示加载进度,加载完成后进入页面内容,带颜色的进度条总是少不了的,那样子看起来也舒服,如何实现自定义手机浏览器功能呢?

上面是360浏览器加载过程的截图,看起来也不算复杂,在做安卓开发中,经常要用到浏览器加载HTML的页面,于是想做一个demo,避免每次重复写的麻烦,效果图如下:


第一步:自定义WebView,命名ProgressWebView,在自定义ProgressWebView中添加进度条效果,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        progressbar = new ProgressBar(context, null,
                android.R.attr.progressBarStyleHorizontal);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                10, 0, 0));
         
        Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states);
        progressbar.setProgressDrawable(drawable);
        addView(progressbar);
        // setWebViewClient(new WebViewClient(){});
        setWebChromeClient(new WebChromeClient());
        //是否支持缩放
        getSettings().setSupportZoom(true);
        getSettings().setBuiltInZoomControls(true);
    }

在这个构造方法里面,自定义进度条属性,设置为水平进度条,进度条的高度,同时定义进度条状态颜色,写在progress_bar_states.xml文件中,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="2dp" />
 
            <gradient
                android:angle="270"
                android:centerColor="#E3E3E3"
                android:endColor="#E6E6E6"
                android:startColor="#C8C8C8" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="2dp" />
 
                <gradient
                    android:centerColor="#4AEA2F"
                    android:endColor="#31CE15"
                    android:startColor="#5FEC46" />
            </shape>
        </clip>
    </item>
 
</layer-list>

在这个xml文件中,可以按照自己喜好设置加载颜色,然后把进度条视图添加到WebView视图中,在使用ProgressWebView加载
HTML网页,可以像360浏览器一样显示加载进度。setWebChromeClient(new
WebChromeClient())用于加载请求的网页,支持进度条、js等效果,这里定义一个内部类WebChromeClient,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
 
    }

这两个getSettings().setSupportZoom(true)和getSettings().setBuiltInZoomControls(true)设置是否支持缩放。
第二步:定义显示类,命名ProgressWebActivity.java,布局文件命名main_web.xml,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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" >
 
    <include
        android:id="@+id/head_views_main"
        layout="@layout/head_re" />
 
    <com.sinolvc.zspg.view.ProgressWebView
        android:id="@+id/baseweb_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fadeScrollbars="true"
        android:scrollbarStyle="insideOverlay" />
 
</LinearLayout>

ProgressWebActivity.java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class ProgressWebActivity extends BaseActivity {
 
    protected ProgressWebView mWebView;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baseweb);
 
        mWebView = (ProgressWebView) findViewById(R.id.baseweb_webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        initData();
         
         
        initTitleView(getWindow().getDecorView(),R.string.load_news_detail_info,ProgressWebActivity .this);
        finishMySelf();
    }
    private void finishMySelf(){
        backll.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                ProgressWebActivity .this.finish();
            }
        });
    }
    protected void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String url = bundle.getString("url");
 
        if(!TextUtils.isEmpty(url)&&!TextUtils.isEmpty(title)){
        mWebView.loadUrl(url);
 
        }
 
    }
 
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mWebView = null;
 
    }
 
}

initData方法获取上一个Activity传递过来的Intent数据,取出网页URL,判断连接是否为空,如果不为空,则使用自定义的ProgressWebView的loadUrl()方法加载,这个时候我们将会在APP端看到如下效果:

initTitleView用于设置浏览器顶部导航条,显示返回按钮和浏览新闻文字。
第三步:在需要使用自定义浏览器这个类ProgressWebActivity的地方,我们只需要设置Intent的数据,然后启动ProgressWebActivity加载之定义URL,实现带进度条加载指定页面的功能。

1
2
3
4
5
6
7
8
Bundle bundle = new Bundle();
 
        bundle.putString("url", "http://www.teachcourse.cn");
        bundle.putString("title", "做最好的源码分享网站");
        Intent intent = new Intent(getContext(), ProgressWebActivity.class);
        intent.putExtras(bundle);
 
        startActivity(intent);

到这里,我们使用ProgressBar+WebView自定义浏览器器的功能基本完成!

ProgressBar+WebView实现自定义浏览器的更多相关文章

  1. 拦截webview调用系统浏览器打开链接

    给WebView设置自定义的WebViewClient即可 webview.setWebViewClient(new WebViewClient(){ @Override public boolean ...

  2. javascript自定义浏览器右键菜单

    javascript自定义浏览器右键菜单   在书上看到document对象还有一个contextmenu事件,但是不知为什么w3school中找不到这个耶... 利用这个特性写了个浏览器的右键菜单, ...

  3. 不使用webview,用手机浏览器的android app

    需求:wap站在手机上以App的形式打开,但不要嵌套WebView,只能以浏览器打开 package com.gzz.whyinzi; import android.net.Uri; import a ...

  4. 自定义浏览器滚动条的样式,打造属于你的滚动条风格——兼容IE和webkit(ff不支持)

    前段时间,到网上找素材时,看到了一个很个性的滚动条式,打开Chrome的调试工具看了一下,发现不是用JavaScript来模拟实现的,觉得 有必要折腾一下.于是在各大浏览器中对比了一下,发现只用Chr ...

  5. 在IE中启动火狐——自定义浏览器链接协议

    有时候需求就是这么奇葩,特别是在这个浏览器混战收尾的节骨眼上,有的客户正在将全单位的浏览器统一到Chrome.有的正在统一到Firefox.还有的正在统一到360上.于是就有了如题的需求,客户正在将浏 ...

  6. [android] WebView自定义浏览器

    在布局文件中添加<EditText/>和<Button/>控件, 在布局文件中添加<WebView/>控件 在Activity中获取WebView对象 调用WebV ...

  7. 001 - 使用鸿蒙WebView创建简单浏览器 step 1

    打开官网,找到WebView的文档(模拟器不支持) 鸿蒙webview的开发指南(原始链接,方便大家识别并点击):https://developer.harmonyos.com/cn/docs/doc ...

  8. JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome

    今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色.但是css ...

  9. Android ProgressBar分析及自定义ProgressBar

    ProgressBar是在执行耗时操作时的一种人性化设计.分为两种形式:转圈的,能显示进度的. 而能取决于是什么样式的PregressBar,当然就是PregressBar的样式啦~ Widget.P ...

随机推荐

  1. POJ 3368 Frequent values 线段树与RMQ解法

    题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...

  2. CodeForces - 361D Levko and Array

    Discription Levko has an array that consists of integers: a1, a2, ... , an. But he doesn’t like this ...

  3. 使用log4net无法将日志记录插入mysql数据库解决办法

    写在前面 今天没事研究了下,将日志文件写入mysql数据库,因为新公司用的数据库也是mysql,项目中需要将日志信息写入数据库,没办法,就研究了下.在使用过程中遇到一个很蛋疼的问题.最后解决了,郁闷了 ...

  4. weblogic控制台登录很慢

      分类: Oracle 原文地址:weblogic控制台登录很慢 作者:paomananshan 实际是JVM在Linux下的bug 他想调用一个随机函数 但取不到 暂时的解决办法是 1)较好的解决 ...

  5. spring与struts2整合出现常见错误

    错误信息 严重: Exception starting filter struts2 Unable to load configuration. - bean - jar:file:/F:/Strut ...

  6. tomcat使用安全及CVE-2017-12615

    tomcat安全情报的收集 1.首先定期查看官网各个版本存在的安全漏洞公告: http://tomcat.apache.org/security.html 2.去各大漏洞网站查看漏洞披露信息 看几个漏 ...

  7. 面向对象在JavaScript中的接口实现

    接口是面向对象编程的基础.它是一组包括了函数型方法的数据结构,与类一样.都是编程语言中比較抽象的概念.比方生活中的接口.机顶盒.人们利用它来实现收看不同频道和信号的节目,它宛如对不同类型的信息进行集合 ...

  8. python多进程生成缩略图

    在img目录下7张图片 分别是 11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg #encoding=utf-8 import os import ti ...

  9. VTK学习之路——画画我的小苹果

    数据集主要由描写叙述数据集几何形状的点集数据及构成数据集的单元构成,因此构建数据集的主要任务就是确定点集和构建单元,本演示样例程序构建了一个苹果的实体,然后绘制苹果.演示样例程序运行的过程例如以下: ...

  10. jquery中的clone()方法

    jquery中不能直接把选择到的元素如$('div')添加到其他地方,而需要使用$('div')[0]等 clone()方法直接复制HTML代码,所以可以直接用来添加元素.