SoFunction
Updated on 2025-04-10

Android switch card TabWidget usage example

This article describes the usage of TabWidget for Android switching card. Share it for your reference, as follows:

The Tab tab is similar to the interface of the phone book. It switches different contents through multiple tags. To achieve this effect, you must first know that TabHost is a container used to store multiple Tab tags. Each Tab can correspond to its own layout. For example, the Tab layout in the phone book is a linear layout.

To use TabHost, first you need to get the TabHost object through the getTabHost method, and then add Tab to TabHost through the addTab method. Of course, each Tab will generate an event when switching. To catch this event, you need to set the event listening of TabActivity setOnTabChangedListener

Here is a small example:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class TabTest extends TabActivity {
  /** Called when the activity is first created. */
  TabHost tabhost;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    //Get TabHost object    tabhost = getTabHost();
    //Add a tag to TabHost    // Create a newTabSpec(newTabSpec)    //Set its labels and icons (setIndicator)    //Set Content (setContent)    (("tab1")
        .setIndicator("TAB 1",getResources().getDrawable(.img1))
        .setContent(.text1));
    (("tab2")
        .setIndicator("TAB 2",getResources().getDrawable(.img2))
        .setContent(.text2));
    (("tab3")
        .setIndicator("TAB 3",getResources().getDrawable(.img3))
        .setContent(.text3));
    //Set the background color of TabHost    //((150,22,70,150));
    //Set the background image resources of TabHost    (.bg0);
    //Set which tag is currently displayed    (0);
    //Tag switch event processing, setOnTabChangedListener    (new OnTabChangeListener()
    {
      public void onTabChanged(String tabId)
      {
        Toast toast=(getApplicationContext(), "It's now"+tabId+"Label", Toast.LENGTH_SHORT);
        ();
      }
    });
  }
}

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:andro
  android:
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
      android:
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
    <FrameLayout
      android:
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <TextView
        android:
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Tab 1" />
      <TextView
        android:
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Tab 2" />
      <TextView
        android:
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Tab 3" />
    </FrameLayout>
  </LinearLayout>
</TabHost>

For more information about Android related content, please check out the topic of this site:Android control usage summary》、《Android View View Tips Summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial"and"Android resource operation skills summary

I hope this article will be helpful to everyone's Android programming design.