SoFunction
Updated on 2025-04-09

Detailed explanation of how to use Android Tabhost

There are two ways to implement tab view on Android.A sort ofIt's in the layout pageDefine the <tabhost> tag,Anotherthat isInherit tabactivity.But I prefer the second method, which is that if the page is more complicated, your XML file will be written in a large way, and the XML page is much more concise in the second way.

Here is my XML source code:

<FrameLayout 
 xmlns:andro 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
 <ListView 
   android: 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:cacheColorHint="#FFFFFFFF" 
   android:scrollbars="vertical" 
   android:paddingTop="5dip" 
   android:paddingBottom="5dip" 
   android:paddingRight="5dip" 
   android:background="#FFFFFFFF" 
   android:listSelector="@drawable/list_item_selecter" 
   /> 
 <ListView 
   android: 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:cacheColorHint="#FFFFFFFF" 
   android:scrollbars="vertical" 
   android:paddingTop="5dip" 
   android:paddingBottom="5dip" 
   android:paddingRight="5dip" 
   android:background="#FFFFFFFF" 
   /> 
 <ListView 
   android: 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:cacheColorHint="#FFFFFFFF" 
   android:scrollbars="vertical" 
   android:paddingTop="5dip" 
   android:paddingBottom="5dip" 
   android:paddingRight="5dip" 
   android:background="#FFFFFFFF" 
   /> 
 <ListView 
   android: 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:cacheColorHint="#FFFFFFFF" 
   android:scrollbars="vertical" 
   android:paddingTop="5dip" 
   android:paddingBottom="5dip" 
   android:paddingRight="5dip" 
   android:background="#FFFFFFFF" 
   /> 
</FrameLayout> 

This is the JAVA source code:

private TabHost tabHost; 
private ListView listView; 
private MyListAdapter adapter; 
private View footerView; 
private List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
 (savedInstanceState); 
 tabHost = (); 
 
 (this).inflate(, 
   (), true); 
 
 (("tab1").setIndicator("", 
   getResources().getDrawable()).setContent( 
   .journals_list_one)); 
 (("tab2").setIndicator("", 
   getResources().getDrawable()).setContent( 
   .journals_list_two)); 
 (("tab3").setIndicator("", 
   getResources().getDrawable()).setContent( 
   .journals_list_three)); 
 (("tab4").setIndicator("", 
   getResources().getDrawable()).setContent( 
   .journals_list_end)); 
 
 (0); 
 setContentView(tabHost); 
 (tabChangeListener); 
 
 showContent(); 
 
} 

Let your class inherit TabActivity, then get the tabhost object by calling the getTabHost() method, and then load the layout file of the data displayed by you written into the tabhost, which can be achieved. Finally, add relevant attributes of the tag by calling the addTab() method (such as: tag name, tag picture, tag content layout).

If you configure tabHost through XML files, you need to note that the ids of framelayout and tabwidge tags must refer to the system id (@android:id/tabcontent,@android:id/tabs), otherwise an exception will be reported. In the program, use findViewById() to load tabhost, and then call the () method to initialize tabhost. The subsequent steps are the same as the above, so I will not explain it.
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.