天天看點

Android自定義标題欄:顯示網頁加載進度

這陣子在做lephone的适配,測試組送出一個bug:标題欄的文字較長時沒有顯示完全,其實這并不能算個bug,并且這個問題在以前其他機器也沒有出現,隻是說在lephone的這個平台上顯示得不怎麼美觀,因為聯想将原生的标題欄ui進行了修改。修改的過程中遇到了一個難題,系統自帶的那個标題欄進度總能夠到達100%後漸退,但是我每次最後到100%那一段顯示不全,嘗試了用線程程式死了卡主了不說,還是一樣的效果,後來同僚一句話提醒了我用動畫。确實是這樣我猜系統的也是這樣實作的,等進度到達100%後,用動畫改變它的透明度就ok了。

實作的效果:标題欄顯示網頁标題并且滾動,并且用進度條顯示網頁的加載進度(重新自定義标題欄,lephone修改後的都帶有一個傳回按鈕,并且标題文本和進度條是frame布局的不怎麼好看)。

1、首先定義一個relativelayout布局檔案 broser_custom_title.xml (alwaysmarqueetextview這個類重寫了textview,實作一個跑馬燈的效果,網上能夠找到)

<?xml version="1.0" encoding="utf-8"?>  

<relativelayout  

  xmlns:android="http://schemas.android.com/apk/res/android" 

  android:layout_width="fill_parent" 

  android:layout_height="fill_parent">  

      <com.android.customtitletest  

            android:id="@+id/tvtitle" 

            android:layout_width="fill_parent" 

            android:layout_height="wrap_content" 

 android:focusableintouchmode="true" 

            android:singleline="true" 

 android:ellipsize="marquee" 

            android:focusable="false" 

 android:marqueerepeatlimit="marquee_forever" 

            android:textsize="20sp" 

 android:layout_centervertical="true"/>  

    <progressbar android:id="@+id/pb" 

         android:layout_width="fill_parent" 

 android:layout_height="wrap_content" 

        style="?android:attr/progressbarstylehorizontal" 

        android:visibility="gone" 

 android:layout_alignparentbottom="true" 

                ></progressbar>  

</relativelayout> 

.android.customtitletest>

2、繼承webchromeclient,重寫onprogresschanged和onreceivedtitle事件(進度條加載完成後使用動畫漸退)

public class mywebchromeclient extends webchromeclient {  

    private activity activity;  

    private progressbar pb;  

    private textview tvtitle;  

    public mywebchromeclient(activity activity) {  

        this.activity = activity;  

    }  

    animation animation;  

      @override 

    public void onprogresschanged(webview view, int newprogress) {  

        pb=(progressbar)activity.findviewbyid(r.id.pb);  

        pb.setmax(100);  

        if(newprogress<100){  

            if(pb.getvisibility()==view.gone)  

                pb.setvisibility(view.visible);  

            pb.setprogress(newprogress);  

        }else{  

            pb.setprogress(100);  

            animation=animationutils.loadanimation(activity, r.anim.animation);  

            // 運作動畫 animation  

              pb.startanimation(animation);  

              // 将 spinner 的可見性設定為不可見狀态  

              pb.setvisibility(view.invisible);  

         }  

                super.onprogresschanged(view, newprogress);  

    @override 

    public void onreceivedtitle(webview view, string title) {  

        tvtitle=(textview)activity.findviewbyid(r.id.tvtitle);  

        tvtitle.settext(title);  

        super.onreceivedtitle(view, title);  

3、進度條的動畫樣式 res/anim/animation.xml

<?xml version="1.0" encoding="utf-8"?> 

    <set xmlns:android="http://schemas.android.com/apk/res/android"> 

         <alpha android:fromalpha="1.0" android:toalpha="0.0" android:duration="700"/> 

   </set> 

4、碼設定自定義的标題欄

private webview browser;  

@override 

public void oncreate(bundle savedinstancestate)  

      super.oncreate(savedinstancestate);  

    getwindow().requestfeature(window.feature_custom_title);  

    setcontentview(r.layout.main);  

    getwindow().setfeatureint(window.feature_custom_title, r.layout.broser_custom_title);  

    browser = (webview) findviewbyid(r.id.my_browser);  

    // currentwebview=browser;  

    browser.setwebchromeclient(new mywebchromeclient(main.this));  

    browser.loadurl("http://www.163.com");  

繼續閱讀