Nowadays, fragment is used more and more widely, although the fragment parasite is under activity. But its presence is a fortunate thing for developers to make development more efficient. All right, here's how to use Fragmenttabhost. Since Tabhost has not been recommended, it is now generally used fragmenttabhost!
I am also a rookie, is to help the novice, because fragment is 3.0 only appear, in order to avoid the use of less than 3.0, so we have to use V4 package to support. Don't pour the wrong bag. Big God don't squirt!
One: first we look at XML:
1.activity_main.xml
<?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 "> & Lt 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: I D/tabhost "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:backgroun d= "@drawable/bg_tabhost_bg" > <framelayout android:id= "@android: Id/tabcontent" android:l Ayout_width= "0DP" android:layout_height= "0DP" android:layout_weight= "0"/> </an Droid.support.v4.app.fragmenttabhost></linearlayout>
2.tab_item_view.xml
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:gravity=" center " android:orientation=" Vertical "> <imageview android:id=" @+id/imageview " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:focusable= "false" android:padding= "3DP" android:src= "@drawable/tab_home_btn" > </ImageView> <textview android:id= "@+id/textview" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "" Android:textsize= "10SP" android:textcolor= "#ffffff" > </TextView></LinearLayout>
3.fragment1.xml just stick a fragment XML! The others are the same. Just the color is not the same, hehe.
<?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:orientation=" vertical " android:background=" # fbb55d "> </LinearLayout>
Ok,xml finished first, let's look at the code!
4.mainactivity
Package Com.example.fragmenttabhost;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmenttabhost;import Android.view.layoutinflater;import Android.view.view;import Android.widget.imageview;import Android.widget.tabhost.tabspec;import Android.widget.textview;import Com.example.fragment.fragment1;import Com.example.fragment.fragment2;import Com.example.fragment.fragment3;import com.example.fragment.Fragment4; Import com.example.fragment.fragment5;/** * * @author zqy * */public class Mainactivity extends Fragmentactivity {/** * Fragmenttabhost */private fragmenttabhost mtabhost;/** * Layout filler * */private layoutinflater mlayoutinflater;/** * Fragment number Group interface * */private Class mfragmentarray[] = {fragment1.class, fragment2.class,fragment3.class, Fragment4.class, Fragment5. Class};/** * Storage image array * */private int mimagearray[] = {R.DRAWABLE.TAB_HOME_BTN,R.DRAWABLE.TAB_MESSAGE_BTN, R.drawable.tab _selfinFO_BTN,R.DRAWABLE.TAB_SQUARE_BTN, r.drawable.tab_more_btn};/** * Elective card text * */private String mtextarray[] = {"Home", "message", " Friends "," search "," many other "};/** * * */public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcon Tentview (R.layout.activity_main); Initview ();} /** * Initialize component */private void Initview () {mlayoutinflater = Layoutinflater.from (this);//Find Tabhostmtabhost = (fragmenttabhos T) Findviewbyid (Android. R.id.tabhost); Mtabhost.setup (this, Getsupportfragmentmanager (), r.id.realtabcontent);//Gets the number of fragment int count = mfragmentarray.length;for (int i = 0; i < count; i++) {//sets the icon, text, and content for each Tabbutton tabspec Tabspec = Mtabhost.newtabspec ( Mtextarray[i]). Setindicator (Gettabitemview (i));//Add Tabbutton into Tab tab mtabhost.addtab (Tabspec, Mfragmentarray[i] , null);//Set Tabbutton background Mtabhost.gettabwidget (). Getchildat (i). Setbackgroundresource (r.drawable.selector_tab_ background);}} /** * * For each Tabbutton set icon and text */private view Gettabitemview (int index) {View view = MlayoutinflatEr.inflate (R.layout.tab_item_view, null); ImageView ImageView = (ImageView) View.findviewbyid (R.id.imageview); Imageview.setimageresource (Mimagearray[index]); TextView TextView = (TextView) View.findviewbyid (R.id.textview); Textview.settext (Mtextarray[index]); return view;}}5.fragment1.java fragment the other several are the same, referring to just XML is not the same!
Package Com.example.fragment;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Com.example.fragmenttabhost.r;public class Fragment1 extends fragment{@Overridepublic View Oncreateview ( Layoutinflater Inflater, ViewGroup container,bundle savedinstancestate) {return inflater.inflate (R.layout.fragment1, null);}}
OK basically finished, let's look at the effect!
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqveglhb3l1yw41mte=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
Haha, the effect is still able! All right, go eat!
Resources
Replace Tabhost with Fragmenttabhost