Android开发模板------WebView加载时显示ProgressBar进度条

ProgressBar进度条不能完全填充控件,这让我纠结了很久。

后来ProgressBar添加了一个属性android:progressDrable解决了。

1、首先看下该布局:

<ProgressBar 
	    android:id="@+id/myProgressBar"
	    style="?android:attr/progressBarStyleHorizontal"
	    android:layout_below="@id/control_search_bar"
	    android:layout_gravity="center_vertical"
	    android:visibility="invisible"
	    android:progressDrawable="@drawable/progressbar"
	    android:layout_width="match_parent"
	    android:layout_height="2dp"/>

2、progressbar.xml位于drawable文件夹下:

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

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="2dip" />

                <gradient
                    android:angle="0"
                    android:endColor="#8000ff00"
                    android:startColor="#80ff0000" />
            </shape>
        </clip>
    </item>

</layer-list>
该文件可以设置你自己想要的样式,下面是参考网上样式:

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

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="2dip" />

            <gradient
                android:angle="0"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:startColor="#ff9d9e9d" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="2dip" />

                <gradient
                    android:angle="0"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:startColor="#80ffd300" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="2dip" />

                <gradient
                    android:angle="0"
                    android:endColor="#8000ff00"
                    android:startColor="#80ff0000" />
            </shape>
        </clip>
    </item>

</layer-list>
3、ProgressBar主要是在WebChromeClient使用:

private class MyWebChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			myProgressBar.setMax(100);
			if (newProgress < 100) {
				if (myProgressBar.getVisibility() == View.INVISIBLE) {
					myProgressBar.setVisibility(View.VISIBLE);
				}
				myProgressBar.setProgress(newProgress);
			} else {
				myProgressBar.setProgress(100);
				myProgressBar.setVisibility(View.INVISIBLE);
			}
			super.onProgressChanged(view, newProgress);
		}

	}

写完,收工!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值