android自定义标题栏(带进度条)

自定义标题栏一共有四步,下面逐步介绍:
一:在strings.xml文件中配置标题栏的背景和大小
  1. <style name="CustomWindowTitleBackground">  
  2.     <!-- 背景图片 -->  
  3.     <item name="android:background">@drawable/home_top</item>      
  4. </style>  
  5.   
  6. <style name="title_bar" parent="android:Theme">  
  7.     <item name="android:windowTitleSize">40dp</item>  
  8.     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>  
  9. </style>  

二:配置AndroidMainfest.xml文件中相应的activity:
  1. <activity android:name=".activity.PlazaActivity" >  
  2. <span style="white-space:pre">  </span><intent-filter>  
  3.             <action android:name="android.intent.action.MAIN"  
  4.                 android:theme="@style/title_bar" />  
  5.             <category android:name="android.intent.category.LAUNCHER" />  
  6.         </intent-filter>  
  7. </activity>  



三:在layout文件夹中添加自定义标题栏的布局文件:title_bar.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/tvTitle"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="40dp"  
  10.         android:background="@drawable/home_top"  
  11.         android:gravity="center"  
  12.         android:textColor="#ffffff"  
  13.         android:textSize="30sp" >  
  14.     </TextView>  
  15.     <!-- 圆形进度条 -->  
  16.     <ProgressBar  
  17.         android:id="@+id/progressBar"  
  18.         android:layout_width="30dp"  
  19.         android:layout_height="30dp"  
  20.         android:layout_alignParentRight="true"  
  21.         android:layout_marginTop="5dp" >  
  22.     </ProgressBar>  
  23.   
  24. </RelativeLayout>  

四:准备就绪,最后在相应的activity中声明使用自定义标题栏,例如MainActivity.java:
  1.       public void onCreate(Bundle savedInstanceState) {  
  2. super.onCreate(savedInstanceState);  
  3. // 声明使用自定义标题栏,注意代码的顺序  
  4. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  5. setContentView(R.layout.plaza);  
  6. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,  
  7.         R.layout.title_bar);  
  8.       }  
四步完成自定义标题栏。

因为上述例子我用到了圆形的进度条,就简单介绍下它的使用:

进度条布局文件:progress.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content">  
  5.     <ProgressBar android:id="@+id/progress"  
  6.             android:layout_width="wrap_content"  
  7.             android:layout_height="wrap_content"       
  8.             android:layout_gravity="center_vertical"  
  9.             style="?android:attr/progressBarStyleSmallTitle">  
  10.     </ProgressBar>  
  11. </LinearLayout>  



在相应的activity中声明:
  1. public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.          // 声明使用系统默认的圆形进度条。  
  4.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
  5.         setContentView(R.layout.main);  
  6.         getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);  
  7.         setProgressBarIndeterminateVisibility(true); //开始进度条  
  8.         setProgressBarIndeterminateVisibility (false); //结束进度条   
  9. }  
posted @ 2013-02-27 17:55  vincent_hv  阅读(224)  评论(0编辑  收藏  举报