FragmentTabHost的簡單使用tab例子


效果圖示例:java

 

 

 

 

 


一、res/layout下2個佈局activity_main.xml和textview.xmlandroid


textview.xml佈局app


代碼ide


<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" />this

</LinearLayout>spa

 

 

==================xml

 

二、activity_main.xml佈局get


代碼it

 

<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>

 

 

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

 


三、源文件有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); }}

相關文章
相關標籤/搜索