底部Tab的实现(tabhost)

来源:互联网 发布:c语言流程图规范 编辑:程序博客网 时间:2024/05/02 01:22

国际惯例先上图:


tabhost布局文件,注意tabhost,tabcontent和tabs这三个id一定要正确

源码打印?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.     <RelativeLayout android:orientation="vertical"    
  7.         android:layout_width="match_parent"   
  8.         android:layout_height="match_parent">  
  9.         <FrameLayout android:id="@android:id/tabcontent"    
  10.                 android:layout_width="match_parent"   
  11.                 android:layout_height="match_parent" />  
  12.             <TabWidget android:id="@android:id/tabs"  
  13.                 android:layout_width="match_parent"  
  14.                 android:layout_height="wrap_content"   
  15.                 android:background="@drawable/selector"  
  16.                 android:layout_alignParentBottom="true">  
  17.             </TabWidget>  
  18.     </RelativeLayout>  
  19. </TabHost>  

每一个tab项的布局文件,上面图片下面是文字, 

源码打印?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <ImageView android:id="@+id/tab_iv_icon"  
  7.         android:layout_width="match_parent"   
  8.         android:layout_height="28.0dip"   
  9.         android:scaleType="fitCenter"/>  
  10.     <TextView android:id="@+id/tab_tv_text"  
  11.         android:layout_width="match_parent"   
  12.         android:layout_height="wrap_content"   
  13.         android:textSize="12.0sp"   
  14.         android:textColor="#FFFFFF"  
  15.         android:ellipsize="marquee"   
  16.         android:gravity="center"   
  17.         android:singleLine="true"   
  18.         android:marqueeRepeatLimit="1"/>  
  19. </LinearLayout>  

最后是MainActivity继承自TabActivity

源码打印?
  1. public class MainActivity extends TabActivity {  
  2.     private TabHost tabHost;  
  3.     private static final String HOME = "主页";    
  4.     private static final String REFER = "提及";    
  5.     private static final String ABOUT = "关于";  
  6.     private static final String SEARCH = "搜索";    
  7.     private static final String MORE = "更多";   
  8.     //内容Intent  
  9.     private Intent homeIntent;  
  10.     private Intent referIntent;  
  11.     private Intent aboutIntent;  
  12.     private Intent searchIntent;  
  13.     private Intent moreIntent;  
  14.   
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.tabhost);//设置TabHost使用的布局文件  
  18.           
  19.         tabHost=this.getTabHost();  
  20.         tabHost.setFocusable(true);  
  21.         prepareIntent();  
  22.         setupIntent();  
  23.           
  24.     }  
  25.       
  26.     private void setupIntent(){  
  27.         tabHost.addTab(buildTabSpec(HOME,R.drawable.icon_1_n, homeIntent));  
  28.         tabHost.addTab(buildTabSpec(REFER,R.drawable.icon_2_n, referIntent));  
  29.         tabHost.addTab(buildTabSpec(ABOUT,R.drawable.icon_3_n, aboutIntent));  
  30.         tabHost.addTab(buildTabSpec(SEARCH,R.drawable.icon_4_n, searchIntent));  
  31.         tabHost.addTab(buildTabSpec(MORE,R.drawable.icon_5_n, moreIntent));  
  32.     }  
  33.   
  34.     private TabSpec buildTabSpec(String tag, int icon, Intent intent) {  
  35.         View view = View.inflate(MainActivity.this, R.layout.tab, null);  
  36.         ((ImageView)view.findViewById(R.id.tab_iv_icon)).setImageResource(icon);  
  37.         ((TextView)view.findViewById(R.id.tab_tv_text)).setText(tag);  
  38.         return tabHost.newTabSpec(tag)  
  39.                 .setIndicator(view)  
  40.                 .setContent(intent);  
  41.     }  
  42.   
  43.     private void prepareIntent() {  
  44.         homeIntent=new Intent(this, HomeActivity.class);  
  45.         referIntent=new Intent(this, ReferActivity.class);  
  46.         aboutIntent=new Intent(this, AboutActivity.class);  
  47.         searchIntent=new Intent(this,SearchActivity.class);  
  48.         moreIntent=new Intent(this, MoreActivity.class);  
  49.     }  
  50.   
  51. }  

0 0
原创粉丝点击