Android 分页控件制成底部菜单


其实Android 中的底部菜单, 可以用分页控件很好的实现。  我们先将自定义分页控件做好, 就可以做到顶底两个位置的菜单了。

TabHost只是作为一个容器来存放一些Activity, 所以需要自己另外创建几个新的Activity, 然后由主TabHost加载。

 

tab_style.xml 

是每个Tab的自定义样式

  1. //分页控件样式 
  2. <?xml version="1.0" encoding="UTF-8"?> 
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:layout_width="wrap_content" 
  5.     android:layout_height="wrap_content" 
  6.     android:paddingLeft="5dip" 
  7.     android:paddingRight="5dip" 
  8.     android:paddingTop="5dip" 
  9.     android:background="@drawable/tab_bg" 
  10.     > 
  11.      
  12.     <FrameLayout   
  13.         android:layout_width="fill_parent" 
  14.         android:layout_height="fill_parent" 
  15.         android:layout_weight="0.6" 
  16.         > 
  17.         <TextView   
  18.             android:id="@+id/tab_label" 
  19.             android:layout_width="fill_parent" 
  20.             android:layout_height="fill_parent" 
  21.             android:gravity="center" 
  22.             android:background="@drawable/tab_title_selector" 
  23.             android:textColor="#FFFFFF" 
  24.             android:textStyle="bold" 
  25.         /> 
  26.     </FrameLayout>     
  27. </LinearLayout> 

main_tab.xml  是主TabHost布局文件

  1. //TabHost布局 
  2. <?xml version="1.0" encoding="UTF-8"?> 
  3. <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:id="@android:id/tabhost"   
  5.     android:layout_width="fill_parent" 
  6.     android:layout_height="fill_parent" 
  7.     > 
  8.      
  9.     //必须包含下列三个View 
  10.     <LinearLayout 
  11.         android:orientation="vertical" 
  12.         android:layout_width="fill_parent" 
  13.         android:layout_height="fill_parent" 
  14.         > 
  15.  
  16. <FrameLayout   
  17.             android:gravity="center"   
  18.             android:id="@android:id/tabcontent" 
  19.             android:layout_width="fill_parent" 
  20.             android:layout_height="wrap_content" 
  21.             android:layout_weight="1.0" 
  22.         /> 
  23.          
  24.         //TabWidget位置在FrameLayout之下则显示在低部, 在之上则显示在顶部 
  25.         <TabWidget   
  26.             android:id="@android:id/tabs" 
  27.             android:layout_height="wrap_content" 
  28.             android:layout_width="fill_parent" 
  29.             android:layout_weight="0.0" 
  30.             /> 
  31.          
  32.     </LinearLayout>     
  33.      
  34. </TabHost> 

 

tab_title_selector.xml

是Tab中TextView的按下背景

  1. //选择器,指示Text按下后的背景 
  2. <?xml version="1.0" encoding="UTF-8"?> 
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  4.     <item 
  5.         android:state_focused="true" 
  6.         android:drawable="@drawable/tab_btn_bg_d" 
  7.         /> 
  8.     <item 
  9.         android:state_selected="true" 
  10.         android:drawable="@drawable/tab_btn_bg_d" 
  11.         /> 
  12.     <item 
  13.         android:state_pressed="true" 
  14.         android:drawable="@drawable/tab_btn_bg_d" 
  15.         /> 
  16. </selector> 

Activity类

另外还需要几个Activity类, 普通的Activity类即可, 在此不显示。

  1. public class TabTest extends TabActivity 
  2.     private TabWidget mTabWidget; 
  3.     private TabHost mTabHost; 
  4.     /** Called when the activity is first created. */ 
  5.     @Override 
  6.     public void onCreate(Bundle savedInstanceState) 
  7.     { 
  8.         super.onCreate(savedInstanceState); 
  9.         setContentView(R.layout.main_tabs); 
  10.          
  11.         mTabHost = getTabHost(); 
  12.          
  13.         //将要显示的Activity载入TabHost控件  
  14.         //要显示的Activity由自己自由创建  
  15.         setTabIndicator("one"1new Intent(this, OneActivity.class)); 
  16.         setTabIndicator("Two"2new Intent(this, TwoActivity.class)); 
  17.         setTabIndicator("Three"3new Intent(this, OneActivity.class)); 
  18.         setTabIndicator("Four"4new Intent(this, TwoActivity.class)); 
  19.     } 
  20.      
  21.     private void setTabIndicator(String title, int nId, Intent intent) 
  22.     { 
  23.         //使用指定Tab样式  
  24.         View view = LayoutInflater.from(this.mTabHost.getContext()) 
  25.                     .inflate(R.layout.tab_style, null); 
  26.          
  27.         TextView text   = (TextView)view.findViewById(R.id.tab_label); 
  28.         String strId    = String.valueOf(nId); 
  29.          
  30.         text.setText(title); 
  31.          
  32.         //创建一个新Tab  
  33.         TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(strId) 
  34.                         .setIndicator(view).setContent(intent); 
  35.         //加载新Tab  
  36.         mTabHost.addTab(localTabSpec); 
  37.     } 

Android 分页控件制成底部菜单源码下载地址:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /pub/Android源码集锦/2011年/12月/Android 分页控件制成底部菜单/

相关内容