FragmentTabHost的简单使用tab例子

本文介绍如何使用 Android 中的 FragmentTabHost 实现 TabLayout 功能。通过创建多个 Fragment 并结合 TabSpec 进行配置,可以实现不同标签页之间的切换,并展示相应的文本内容。

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


效果图示例:

 

 

212302_bfnm_2542711.png

 

 

212302_VUzP_2542711.png

 


1、res/layout下2个布局activity_main.xml和textview.xml


textview.xml布局


代码


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

 

 

==================

 

2、activity_main.xml布局


代码

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <!-- FragmentTabHost相当于一个容器 -->
    <android.support.v4.app.FragmentTabHost
        android:id="@+id/fragmentTabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/layout_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

</RelativeLayout>

 

 

========================

 


3、源文件有2个MainActivity.java和PagerFragment.java

 


PagerFragment.java类

 

代码

 


public class PagerFragment extends Fragment {

 private int tabIndex;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  tabIndex = getArguments().getInt("tabIndex");
 }
 @Override
 public View onCreateView(LayoutInflater inflater,
   @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.textview,container,false);
  TextView text = (TextView) view.findViewById(R.id.textView);
  switch(tabIndex){
  case 0:
   text.setText("这是记录页面");
   break;
  case 1:
   text.setText("这是联系人页面");
   break;
  case 2:
   text.setText("这是收藏夹页面");
   break;
  case 3:
   text.setText("这是群组页面");
   break;
  }
  return view;
 }
}

 

 

==================

 


MainActivity类


代码

 

 

public class MainActivity extends FragmentActivity {

 private FragmentTabHost tabHost;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.tabHost = (FragmentTabHost) this.findViewById(R.id.fragmentTabHost);
  
  FragmentManager fragmentManager = getSupportFragmentManager();
  //将FragmentTabHost和布局联系起来
  tabHost.setup(this, fragmentManager, R.id.layout_container);
  
  //创建tab
  TabSpec spec1 = tabHost.newTabSpec("a");//参数:标识
  spec1.setIndicator("记录");//设置标题或 图标
  Bundle bundle1 = new Bundle();
  bundle1.putInt("tabIndex", 0);
  
  TabSpec spec2 = tabHost.newTabSpec("b");//参数:标识
  spec2.setIndicator("联系人");//设置标题或 图标
  Bundle bundle2 = new Bundle();
  bundle2.putInt("tabIndex", 1);
  
  TabSpec spec3 = tabHost.newTabSpec("c");//参数:标识
  spec3.setIndicator("收藏夹");//设置标题或 图标
  Bundle bundle3 = new Bundle();
  bundle3.putInt("tabIndex", 2);
  
  TabSpec spec4 = tabHost.newTabSpec("d");//参数:标识
  spec4.setIndicator("群组");//设置标题或 图标
  Bundle bundle4 = new Bundle();
  bundle4.putInt("tabIndex", 3);
  //把TabSpec Fragment Bundle 关联起来
  tabHost.addTab(spec1, PagerFragment.class, bundle1);
  tabHost.addTab(spec2, PagerFragment.class, bundle2);
  tabHost.addTab(spec3, PagerFragment.class, bundle3);
  tabHost.addTab(spec4, PagerFragment.class, bundle4);
 }
}

 

转载于:https://my.oschina.net/u/2542711/blog/615294

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值