FragmentTabHost的简单使用tab例子


效果图示例:

 

 

FragmentTabHost的简单使用tab例子_第1张图片

 

 

 


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);
 }
}

 

你可能感兴趣的:(FragmentTabHost)