Android 自定義標題欄 顯示網(wǎng)頁加載進度的方法實例
這陣子在做Lephone的適配,測試組提交一個bug:標題欄的文字較長時沒有顯示完全,其實這并不能算個bug,并且這個問題在以前其他機器也沒有出現(xiàn),只是說在Lephone的這個平臺上顯示得不怎么美觀,因為聯(lián)想將原生的標題欄UI進行了修改。修改的過程中遇到了一個難題,系統(tǒng)自帶的那個標題欄進度總能夠到達100%后漸退,但是我每次最后到100%那一段顯示不全,嘗試了用線程程序死了卡主了不說,還是一樣的效果,后來同事一句話提醒了我用動畫。確實是這樣我猜系統(tǒng)的也是這樣實現(xiàn)的,等進度到達100%后,用動畫改變它的透明度就ok了。
實現(xiàn)的效果:標題欄顯示網(wǎng)頁標題并且滾動,并且用進度條顯示網(wǎng)頁的加載進度(重新自定義標題欄,lephone修改后的都帶有一個返回按鈕,并且標題文本和進度條是Frame布局的不怎么好看)。
1、首先定義一個RelativeLayout布局文件 broser_custom_title.xml (AlwaysMarqueeTextView這個類重寫了TextView,實現(xiàn)一個跑馬燈的效果,網(wǎng)上能夠找到
<?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.AlwaysMarqueeTextView
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:layout_centerVertical="true"/>
<ProgressBar android:id="@+id/pb"
android:layout_width="fill_parent" android:layout_height="2sp"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone" android:layout_alignParentBottom="true"
></ProgressBar>
</RelativeLayout>
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 的可見性設(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、進度條的動畫樣式 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、碼設(shè)置自定義的標題欄
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.dbjr.com.cn");
}
相關(guān)文章
很贊的引導(dǎo)界面效果Android控件ImageSwitcher實現(xiàn)
這篇文章主要為大家詳細介紹了Android控件ImageSwitcher如何實現(xiàn)很贊的引導(dǎo)界面的具體代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼實例詳解
這篇文章主要介紹了Android通過訪問網(wǎng)頁查看網(wǎng)頁源碼的相關(guān)資料,需要的朋友可以參考下2017-06-06Android Gradle Build Error:Some file crunching failed, see l
這篇文章主要介紹了Android Gradle Build Error:Some file crunching failed, see logs for details解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11Kotlin學(xué)習(xí)筆記之const val與val
這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)筆記之const val與val的相關(guān)資料,并給大家介紹了const val和val區(qū)別以及Kotlin中var和val的區(qū)別,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05