After Android3.0, Google created the fragment, so the original tabhost has not been recommended, now generally recommended to use Fragmenttabhost.
Google considered compatibility issues, so fragmenttabhost was not added to the official SDK, but it was placed in the Android-support-v4.jar
Here I take you to realize this function.
1, Myfragmenttabhostactivity.java
public class Myfragmenttabhostactivity extends Fragmentactivity {private context context;
Private Fragmenttabhost fragmenttabhost = null;
Private class[] Fragmentarray = {myfragment.class, myfragment.class, Myfragment.class, myfragment.class}; Private int[] Tabimagearray = {r.drawable.ic_launcher, r.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_
Launcher};
Private string[] Tabtextarray = {"Square", "rank", "mall", "I"};
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_fragmenttabhost);
context = this;
Initview (); private void Initview () {Layoutinflater inflater = (layoutinflater) getsystemservice (Context.layout_inflater_servic
E); Fragmenttabhost = (fragmenttabhost) Findviewbyid (Android.
R.id.tabhost);
Fragmenttabhost.setup (Context, Getsupportfragmentmanager (), r.id.real_tabcontent);
for (int i = 0; i < fragmentarray.length i++) {//Set icon, text, and content for each tab buttonTabspec Tabspec = Fragmenttabhost.newtabspec (tabtextarray[i)). Setindicator (Gettabitemview (Inflater, i));
Fragmenttabhost.addtab (Tabspec, fragmentarray[i], NULL); Set the background of the tab button Fragmenttabhost.gettabwidget (). Getchildat (i). SetBackgroundColor (Color.rgb ((int) (Math.random () *255)
, (int) (Math.random () *255), (int) (Math.random () *255)); } public view Gettabitemview (layoutinflater inflater, int index) {View view = Inflater.inflate (R.layout.item_tab,
NULL);
ImageView ImageView = (imageview) View.findviewbyid (R.id.imageview);
TextView TextView = (TextView) View.findviewbyid (R.id.textview);
Imageview.setimageresource (Tabimagearray[index]);
Textview.settext (Tabtextarray[index]);
return view; }
}
2, Fragment.java
public class Myfragment extends Fragment {
@Override public
View Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view = inflater.inflate (r.layout.layout_fragment, null);
View.setbackgroundcolor (Color.rgb (int) (Math.random () *255), (int) (Math.random () *255), (int) (Math.random () *255)) ;
return view;
}
3, Activity_fragment.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<framelayout
android:id= "@+id/real_tabcontent"
Match_parent "
android:layout_height=" 0DP "
android:layout_weight=" 1 "/>
< Android.support.v4.app.FragmentTabHost
android:id= "@android: Id/tabhost"
android:layout_width= "Match_ Parent "
android:layout_height=" wrap_content "
android:layout_weight=" 0 ">
<framelayout
android:id= "@android: id/tabcontent"
android:layout_width= "0DP"
android:layout_height= "0DP"/ >
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
4, Item_tab.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<linearlayout
android:layout_width= "wrap_content"
android:layout_ height= "Wrap_content"
android:layout_centerinparent= "true"
android:gravity= "Center_horizontal"
android:orientation= "vertical" >
<imageview
android:id= "@+id/imageview"
android:layout_ Width= "Wrap_content"
android:layout_height= "wrap_content"
android:src= "@drawable/ic_launcher"/>
<textview
android:id= "@+id/textview"
android:layout_width= "Wrap_content"
android: layout_height= "Wrap_content"
android:text= ""/>
</LinearLayout>
</RelativeLayout>
5, Layout_fragment.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
</LinearLayout>
6, run the screenshot