FragmentTabHost simple use

本文介绍如何在Android应用中使用FragmentTabHost替代TabActivity来创建带标签的界面。通过继承FragmentActivity并利用FragmentTabHost组件,可以在应用中实现灵活且可定制的标签式导航效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  最近在学TabHost时发现TabActivity在API level 13以后不用了,所以就去寻找它的替换类,找到FragmentActivity,可以把每个Fragment作为子tab添加到FragmentActivity上。tab可以放在最上面也可以放在最下面由-----

以下布局文件main.xml<FrameLayout>的位置决定

 
  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.       
  7.     <!-- 这个布局决定了标签在上面还是在下面显示 -->  
  8.     <FrameLayout   
  9.         android:id="@+id/realtabcontent"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="0dip"  
  12.         android:layout_weight="1" />  
  13.       
  14.     <android.support.v4.app.FragmentTabHost  
  15.         android:id="@android:id/tabhost"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content">  
  18.           
  19.         <TabWidget   
  20.             android:id="@android:id/tabs"  
  21.             android:layout_width="match_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:orientation="horizontal"/>  
  24.     </android.support.v4.app.FragmentTabHost>  
  25.       
  26. </LinearLayout>  


创建一个类继承FragmentActivity

[java]  view plain  copy
  1. package com.example.tabhostdemo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.FragmentActivity;  
  5. import android.support.v4.app.FragmentTabHost;  
  6. import android.view.View;  
  7. import android.widget.TextView;  
  8.   
  9. import com.example.tabhost.FirstFragment;  
  10. import com.example.tabhost.ThirdFragment;  
  11. import com.example.tabhost.secondFragment;  
  12.   
  13. public class MainActivity extends FragmentActivity {  
  14.   
  15.     private FragmentTabHost mTabHost = null;;  
  16.     private View indicator = null;  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.   
  22.         setContentView(R.layout.main);  
  23.   
  24.         mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);  
  25.         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
  26.   
  27.         // 添加tab名称和图标  
  28.         indicator = getIndicatorView("我的联系人", R.layout.mycontact_indicator);  
  29.         mTabHost.addTab(mTabHost.newTabSpec("myContact")  
  30.                 .setIndicator(indicator), FirstFragment.classnull);  
  31.   
  32.         indicator = getIndicatorView("陌生人", R.layout.strangercontact_indicator);  
  33.         mTabHost.addTab(  
  34.                 mTabHost.newTabSpec("stranger").setIndicator(indicator),  
  35.                 secondFragment.classnull);  
  36.   
  37.         indicator = getIndicatorView("常联系人", R.layout.alwayscontact_indicator);  
  38.         mTabHost.addTab(  
  39.                 mTabHost.newTabSpec("alwaysContact").setIndicator(indicator),  
  40.                 ThirdFragment.classnull);  
  41.     }  
  42.   
  43.     private View getIndicatorView(String name, int layoutId) {  
  44.         View v = getLayoutInflater().inflate(layoutId, null);  
  45.         TextView tv = (TextView) v.findViewById(R.id.tabText);  
  46.         tv.setText(name);  
  47.         return v;  
  48.     }  
  49.   
  50.     @Override  
  51.     protected void onDestroy() {  
  52.         // TODO Auto-generated method stub  
  53.         super.onDestroy();  
  54.         mTabHost = null;  
  55.     }  
  56. }  
第一个Tab的布局文件    存放两张图片,字体颜色

alwayscontact_indicator.xml文件

[html]  view plain  copy
  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:layout_gravity="center"   
  6.     android:gravity="center">  
  7.   
  8.     <TextView   
  9.         android:id="@+id/tabText"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:focusable="true"  
  13.         android:drawableTop="@drawable/mycontact_selector"  
  14.         android:textColor="@drawable/tabitem_txt_sel"/>  
  15. </LinearLayout>  


mycontact_selector.xml文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >      
  3.     <!-- Non focused states -->  
  4.     <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/mycontact" />  
  5.     <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/mycontact_sel" />  
  6.     <!-- Focused states -->  
  7.     <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/mycontact_sel" />  
  8.     <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/mycontact_sel" />  
  9.     <!-- Pressed -->  
  10.     <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/mycontact_sel" />  
  11.     <item android:state_pressed="true" android:drawable="@drawable/mycontact_sel" />    
  12. </selector>  


tabitem_txt_sel.xml文件

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >      
  3.     <!-- Non focused states -->  
  4.     <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:color="#A4A4A4" />  
  5.     <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:color="#00A3F5" />  
  6.     <!-- Focused states -->  
  7.     <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:color="#00A3F5" />  
  8.     <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:color="#00A3F5" />  
  9.     <!-- Pressed -->  
  10.     <item android:state_selected="true" android:state_pressed="true" android:color="#00A3F5" />  
  11.     <item android:state_pressed="true" android:color="#00A3F5" />   
  12. </selector>  
其它的tab文件定义也是类似的,看下最后的效果图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值