給WebView添加漂亮的載入進度條,webview載入進度條

來源:互聯網
上載者:User

給WebView添加漂亮的載入進度條,webview載入進度條

為了增強使用者體驗,所有在WebView頭部給加了個進度條,看起來不錯哦。


布局XMl:activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">        <WebView            android:id="@+id/webView"            android:layout_width="fill_parent"            android:layout_height="fill_parent"/>                <ProgressBar          android:id="@+id/pb"          style="?android:attr/progressBarStyleHorizontal"          android:layout_width="fill_parent"          android:layout_height="3dip"          android:indeterminateOnly="false"          android:max="100"          android:progressDrawable="@drawable/progress_bar_states" >      </ProgressBar></RelativeLayout>

自訂進度條:

<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>

然後就是Activity的主要代碼啦:

ProgressBar pb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.xxx);pb = (ProgressBar) findViewById(R.id.pb);pb.setMax(100);WebView webView = (WebView) findViewById(R.id.webview);webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setSupportZoom(true);  webView.getSettings().setBuiltInZoomControls(true);webView.setWebChromeClient(new WebViewClient() );webView.loadUrl("http://www.x.com");}private class WebViewClient extends WebChromeClient {@Overridepublic void onProgressChanged(WebView view, int newProgress) {pb.setProgress(newProgress);if(newProgress==100){pb.setVisibility(View.GONE);}super.onProgressChanged(view, newProgress);}}

不多說,知道大家最關心效果如何咯:

轉載請註明:破曉部落格 » [原創]給WebView添加漂亮的載入進度條

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.