新聞中心
這陣子在做lephone的適配,測試組提交一個bug:標(biāo)題欄的文字較長時沒有顯示完全,其實這并不能算個bug,并且這個問題在以前其他機(jī)器也沒有出現(xiàn),只是說在Lephone的這個平臺上顯示得不怎么美觀,因為聯(lián)想將原生的標(biāo)題欄UI進(jìn)行了修改。修改的過程中遇到了一個難題,系統(tǒng)自帶的那個標(biāo)題欄進(jìn)度總能夠到達(dá)100%后漸退,但是我每次***到100%那一段顯示不全,嘗試了用線程程序死了卡主了不說,還是一樣的效果,后來同事一句話提醒了我用動畫。確實是這樣我猜系統(tǒng)的也是這樣實現(xiàn)的,等進(jìn)度到達(dá)100%后,用動畫改變它的透明度就ok了。

實現(xiàn)的效果:標(biāo)題欄顯示網(wǎng)頁標(biāo)題并且滾動,并且用進(jìn)度條顯示網(wǎng)頁的加載進(jìn)度(重新自定義標(biāo)題欄,lephone修改后的都帶有一個返回按鈕,并且標(biāo)題文本和進(jìn)度條是Frame布局的不怎么好看)。
1、首先定義一個RelativeLayout布局文件 broser_custom_title.xml (AlwaysMarqueeTextView這個類重寫了TextView,實現(xiàn)一個跑馬燈的效果,網(wǎng)上能夠找到)
- xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- 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"/>
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleHorizontal"
- android:visibility="gone"
- android:layout_alignParentBottom="true"
- >
2、繼承WebChromeClient,重寫onProgressChanged和onReceivedTitle事件(進(jìn)度條加載完成后使用動畫漸退)
- 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);
- // 運(yùn)行動畫 animation
- pb.startAnimation(animation);
- // 將 spinner 的可見性設(shè)置為不可見狀態(tài)
- 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、進(jìn)度條的動畫樣式 res/anim/animation.xml
4、碼設(shè)置自定義的標(biāo)題欄
- 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");
- }
【編輯推薦】
- 深入理解Android消息處理系統(tǒng)原理
- Android用戶界面設(shè)計:框架布局
- Android游戲開發(fā):如何實現(xiàn)爆炸效果
- 盤點(diǎn)Android開發(fā)者必備的十大開發(fā)工具
- Android設(shè)計趨勢分析10則
本文名稱:Android自定義標(biāo)題欄:顯示網(wǎng)頁加載進(jìn)度
鏈接URL:http://www.dlmjj.cn/article/coheodp.html


咨詢
建站咨詢
