Use fragment to achieve the effect of the tab page:
Three pages are separate three fragment
Implementation of the main activity:
PackageCom.hsx.tab;ImportAndroid.os.Bundle;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.app.FragmentManager;Importandroid.support.v4.app.FragmentTransaction;ImportAndroid.view.View;ImportAndroid.widget.ImageButton; Public classMainactivityextendsfragmentactivity {protected Static FinalString TAG = "Mainactivity"; PrivateView Currentbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initcomponents (); } Private voidinitcomponents () {ImageButton Btn_one=(ImageButton) Findviewbyid (R.id.buttom_one); ImageButton Btn_two=(ImageButton) Findviewbyid (r.id.buttom_two); ImageButton Btn_three=(ImageButton) Findviewbyid (R.id.buttom_three); Btn_one.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Fragmentmanager fm=Getsupportfragmentmanager (); Fragmenttransaction ft=fm.begintransaction (); Fragment_one Fragment_one=NewFragment_one (); Ft.replace (R.id.fl_content, Fragment_one, Mainactivity.tag); Ft.commit (); Setbutton (v); } }); Btn_two.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Fragmentmanager fm=Getsupportfragmentmanager (); Fragmenttransaction ft=fm.begintransaction (); Fragment_two Fragment_two=NewFragment_two (); Ft.replace (R.id.fl_content, Fragment_two, Mainactivity.tag); Ft.commit (); Setbutton (v); } }); Btn_three.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Fragmentmanager fm=Getsupportfragmentmanager (); Fragmenttransaction ft=fm.begintransaction (); Fragment_three Fragment_three=NewFragment_three (); Ft.replace (R.id.fl_content, Fragment_three, Mainactivity.tag); Ft.commit (); Setbutton (v); } }); /*** Default First button click*/Btn_one.performclick (); } /*** Set the background image of the button * *@paramv*/ Private voidSetbutton (View v) {if(Currentbutton! =NULL&& Currentbutton.getid ()! =V.getid ()) {currentbutton.setenabled (true); } v.setenabled (false); Currentbutton=v; }}
The code of the XML
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <LinearLayoutAndroid:id= "@+id/buttom_bar_group"Android:layout_width= "Match_parent"Android:layout_height= "60DP"Android:layout_alignparentbottom= "true"android:gravity= "Center_vertical"android:orientation= "Horizontal"> <Relativelayoutstyle= "@style/buttombar"> <ImageButtonAndroid:id= "@+id/buttom_one"style= "@style/buttombarimgbtn"Android:background= "@drawable/bar_news"android:contentdescription= "@string/app_name" /> </Relativelayout> <Relativelayoutstyle= "@style/buttombar"> <ImageButtonAndroid:id= "@+id/buttom_two"style= "@style/buttombarimgbtn"Android:background= "@drawable/bar_constact"android:contentdescription= "@string/app_name" /> </Relativelayout> <Relativelayoutstyle= "@style/buttombar"> <ImageButtonAndroid:id= "@+id/buttom_three"style= "@style/buttombarimgbtn"Android:background= "@drawable/bar_deynaimic"android:contentdescription= "@string/app_name" /> </Relativelayout> </LinearLayout> <ViewAndroid:id= "@+id/line"Android:layout_width= "Match_parent"Android:layout_height= "0.5DP"Android:layout_above= "@id/buttom_bar_group"Android:background= "@color/devide_line" /> <FramelayoutAndroid:id= "@+id/fl_content"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_above= "@id/line" /></Relativelayout>
Demo:http://files.cnblogs.com/hsx514/test_fragment.zip