Since Android3.2, tabactibits are Deprecated and replaced by FragmentActivity. Because Fragment is more flexible than Activiy and consumes less resources, it can fully meet the TabActivity effect, so it is directly replaced. The original TabActibvity + TabHost + Activity can be used. However, we strongly recommend that you use FragmentActivity + FragmentTabHost + Fragement.
FragmentTabHost usage:
1. Define the layout of the FragmentActivity:
[Html]View plaincopy
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: orientation = "vertical">
- <FrameLayout
- Android: id = "@ + id/realtabcontent"
- Android: layout_width = "fill_parent"
- Android: layout_height = "0dip"
- Android: layout_weight = "1"/>
- <Android. support. v4.app. FragmentTabHost
- Android: id = "@ android: id/tabhost"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: background = "@ drawable/maintab_toolbar_bg">
- <FrameLayout
- Android: id = "@ android: id/tabcontent"
- Android: layout_width = "0dp"
- Android: layout_height = "0dp"
- Android: layout_weight = "0"/>
- </Android. support. v4.app. FragmentTabHost>
- </LinearLayout>
2. FragmentActivity must be inherited.
[Java]View plaincopy
- Public class MainTabActivity extends FragmentActivity {
- // Define the FragmentTabHost object
- Private FragmentTabHost mTabHost;
3. Get the FragmentTabHost object.
[Java]View plaincopy
- MTabHost = (FragmentTabHost) findViewById (android. R. id. tabhost );
4. initialize the FragmentTabHost object
[Java]View plaincopy
- MTabHost. setup (this, getsuppfrfragmentmanager (), R. id. realtabcontent );
Note that R. id. realtabcontent can be any ViewGroup or its subclass Object id, such as LinearLayout. The actual function is a container. During Tab switching, the Fragment corresponding to the current Tab will be added to this ViewGroup as its sub-View.
5. Add each Tab page in order
[Java]View plaincopy
- // Set icons, text, and content for each Tab button
- TabSpec tabSpec = mTabHost. newTabSpec ("TAG1"). setIndicator (yourTabItemView );
- // Add the Tab button to the Tab
- MTabHost. addTab (tabSpec, fragmentPage1.class, null );
- // Set the background of the Tab button
- MTabHost. getTabWidget (). getChildAt (I). setBackgroundResource (R. drawable. selector_tab_background );
Note: mTabHost. newTabSpec ("TAG1"). setIndicator (yourTabItemview); here "TAG1" is actually meaningless, just differentiate each tab.
The focus is on the setIndicator function, which has three different implementations. That is to say, you can define your Tab style in three ways:
[Java]View plaincopy
- // Only use the text tab
- TabHost. TabSpec. setIndicator (CharSequence label)
- // Use text + icon to identify the tab
- TabHost. TabSpec. setIndicator (CharSequence label, Drawable icon)
- // Use a custom View to represent a tab
- TabHost. TabSpec. setIndicator (View view)
The preceding two tab styles are the Holo styles we see in most tabhost examples ), that is, there will be a highlighted line (indicator) similar to the scroll bar under the Selected tab. In many cases, we do not need it, such as the style Tab. In this case, you can use the third method to customize your Tab style. You can implement any style Tab:
[Java]View plaincopy
- View yourTabItemView = layoutInflater. inflate (R. layout. tab_item_view, null );
- ImageView imageView = (ImageView) view. findViewById (R. id. imageview );
- ImageView. setImageResource (mImageViewArray [index]);
- TextView textView = (TextView) view. findViewById (R. id. textview );
- TextView. setText (mTextviewArray [index]);
In addition, fragmentPage1.class is a class inherited from Fragment. When you switch the Tab, It is dynamically instantiated and added to the R. id. realtabcontent content container.
After completing the preceding steps, a simple FragementActivity + FragmentTabHost + Fragment will show you how to adjust the Tab to be docked at the top or bottom.
When R. id. realtabcontent and R. id. tabhost are not in the same layout file, the default Tab is under TabContent, And the TabContent and Tab cannot be adjusted.
When R. id. realtabcontent and R. id. when tabhost is in a layout file, if R. id. realtabcontent in R. id. on the tabhost, the Tab will be under TabContent, that is, R. id. realtabcontent and R. id. the relative position of tabhost determines whether the selected page is on or off.
Do not set a sub-View for FragmentTabHost in the layout file. Otherwise, the sub-View is displayed in the View with the coordinates 0 in the upper left corner of the Tab. To R. id. realtabcontent is set to android: layout_weight = "1", because the height of Tabcontent is wrap_content by default and cannot be adjusted. When the Tab is under Tabcontent and the View is insufficient, it is squeezed to the bottom, the Tab will be hung at the end of the displayed View. After setting, the Tabcontent will be filled up.
Source code: http://download.csdn.net/detail/olevin/7563137