SoFunction
Updated on 2025-04-07

Android FragmentTabhost implements navigation pagination

This article shares the specific code displayed on the android FragmentTabhost navigation page for your reference. The specific content is as follows

Basic template

public class MainActivity extends FragmentActivity {
  
  private FragmentTabHost mTabHost;
  private LayoutInflater mLayoutInflater;

  private Class mFragmentArray[] = { , ,
      , ,  };

  private int mImageArray[] = { .tab_home_btn,
      .tab_message_btn, .tab_selfinfo_btn,
      .tab_square_btn, .tab_more_btn };

  private String mTextArray[] = { "front page", "information", "Friends", "search", "More" };

  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    initView();
  }

  private void initView() {

    mLayoutInflater = (this);

    // Find TabHost    mTabHost = (FragmentTabHost) findViewById();
    (this, getSupportFragmentManager(), );
 ().setDividerDrawable(null);//Remove the split line    // Get the number of fragments    for (int i = 0; i < ; i++) {
      // Set icons, text and content for each Tab button      TabSpec tabSpec = (mTextArray[i])
          .setIndicator(getTabItemView(i));
      // Add the Tab button to the Tab tab      (tabSpec, mFragmentArray[i], null);
      // Set the background of the Tab button      ().getChildAt(i)
          .setBackgroundResource(.selector_tab_background);
    }
  }

  //Set icons and text for each Tab button  private View getTabItemView(int index) {
    View view = (.tab_item_view, null);
    ImageView imageView = ();
    //Set the picture selector, select tab to change the icon    switch (index){
      case 0:(.main_bottom_image_selector);break;
      case 1:(.main_bottom_image_selector2);break;
      case 2:(.main_bottom_image_selector3);break;
      case 3:(.main_bottom_image_selector4);break;
      case 4:(.main_bottom_image_selector5);break;
    }
    TextView textView = ();
    (mTextArray[index]);
    //Set the text selector, highlight the selected tab text    (.main_bottom_text_selector);
    return view;
  }

}

acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <FrameLayout
  android:
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1" />

 <.
  android: //The provided id must be used  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:background="@drawable/bg_tabhost_bg">

  <FrameLayout
   android: //The provided id must be used   android:layout_width="0dp"
   android:layout_height="0dp"
   android:layout_weight="0" />      
 </.>

</LinearLayout>

tab_item_view.xml

<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="vertical">
  <ImageView
    android:
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_margin="3dp"
    />
  <TextView
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:textSize="12sp"
    android:layout_marginBottom="2dp"/>
</LinearLayout>

main_bottom_image_selector image selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>
  <item android:state_selected="false" android:drawable="@drawable/ic_launcher" />
  <item android:state_selected="true" android:drawable="@drawable/ic_launcher_round" />
</selector>

Note: The template has 5 tabs, 5 image selectors, 5 text selectors, and 5 fragments need to be created.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.