Explanation of Android Tabhost

  • 2021-07-22 11:26:20
  • OfStack

Android There are two ways to implement the tab view, one is defined in the layout page < tabhost > Tag, the other one is to inherit tabactivity. But I prefer the second way, should be if the page is more complex, your XML file will be larger, with the second way XML page is relatively much more concise.

Here's my XML source:


<FrameLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
 <ListView 
   android:id="@+id/journals_list_one" 
   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:id="@+id/journals_list_two" 
   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:id="@+id/journals_list_three" 
   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:id="@+id/journals_list_end" 
   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 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) { 
 super.onCreate(savedInstanceState); 
 tabHost = this.getTabHost(); 
 
 LayoutInflater.from(this).inflate(R.layout.main, 
   tabHost.getTabContentView(), true); 
 
 tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("", 
   getResources().getDrawable(R.drawable.home)).setContent( 
   R.id.journals_list_one)); 
 tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("", 
   getResources().getDrawable(R.drawable.activity)).setContent( 
   R.id.journals_list_two)); 
 tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("", 
   getResources().getDrawable(R.drawable.community)).setContent( 
   R.id.journals_list_three)); 
 tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("", 
   getResources().getDrawable(R.drawable.shop)).setContent( 
   R.id.journals_list_end)); 
 
 tabHost.setCurrentTab(0); 
 setContentView(tabHost); 
 tabHost.setOnTabChangedListener(tabChangeListener); 
 
 showContent(); 
 
} 

Let your class inherit TabActivity, then get tabhost object by calling getTabHost () method, and then load the layout file of data display written by yourself into tabhost, which can be realized. Finally, the related attributes of the tag (such as tag name, tag picture, tag content layout) are added by calling addTab () method.


Related articles: