The app project has been around for half a month. Slowly familiar with this development environment and development routines.
Although it is stones. But also gradually see the depth of the water!
As an e-commerce project app, it is bound to involve the function of the menu bar. The process of self-realization of this function is rugged, the most complete after the discovery of such a rugged for their own study is very helpful!
For this part of the exploration brought to share with you, hope to help everyone!
Implement app There are many ways to get the bottom menu bar. Tabhost and the Fragment two different ways, and finally succeeded in making Tabhost, to share with you.
In fact the tabhost Implementation of the bottom menu bar is very easy. The real master has built the foundation for us. What we need to do is to be familiar with it and use it so that we can achieve the function we want. So it is very necessary for us to study seriously What is good inside abhost. It is also how to help us achieve the page switch.
first, let's get to know Tabhost Some of the most important functions are indicator (indicators), content, and tag. A tab usually includes a indiicator (indicator), Content (tab), which displays the page view. Can be the ID of this view or a intent), tag.
The Tabspec is used to assist in setting these options:
<1> indicator is generally the name of the tab label and icon;
<2> Content: Can be a view ID, can also create a viewthrough tabcontentfactory , can also be a Intent component to load an activity.
Java class code to use:
<span style= "font-family:kaiti_gb2312;" >package Jczb.shoping.ui;import Android.app.activity;import Android.app.tabactivity;import Android.content.intent;import Android.graphics.color;import Android.os.bundle;import Android.view.KeyEvent;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.widget.imageview;import Android.widget.LinearLayout;import Android.widget.tabhost;import Android.widget.textview;import Android.widget.tabhost.tabspec;public Class Menuactivity extends Tabactivity implements Onclicklistener{linearlayout Llchannel1,llchannel2,llchannel3, Llchannel4;imageview ivmain,ivcart,ivmine;intent itmain,itcart,itmine;int mcurtabid = R.id.channel1;// Define tab four buttonpublic static string tab_tag_anbiaocheck= "Anbiaocheck";p ublic static string tab_tag_settings= "SETTINGS ";p ublic static String tab_tag_download=" DOWNLOAD ";p ublic static tabhost mtabhost;private Animation lefT_in,left_out;private Animation right_in,right_out; @SuppressWarnings ("deprecation") @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_menu); Prepareanim ();p repareintent (); Setupintent (); Findviewbyid (); Initview (); }/* the control by id */protected void Findviewbyid () {Ivmain = (ImageView) Findviewbyid (r.id.imageview1); Ivcart = (ImageView) Findviewbyid (r.id.imageview2); ivmine = (ImageView) Findviewbyid (R.ID.IMAGEVIEW3); llchannel1 = (linearlayout) Findviewbyid (r.id.channel1); Llchannel2 = (linearlayout) Findviewbyid (r.id.channel2); llchannel3 = (linearlayout) Findviewbyid (R.ID.CHANNEL3);} /** * Animation effect */private void Prepareanim () {left_in = Animationutils.loadanimation (this, r.anim.left_in); left_out = Animatio Nutils.loadanimation (this, r.anim.left_out); right_in = Animationutils.loadanimation (this, r.anim.right_in); right_ out = Animationutils.loadanimation (this, r.anim.right_out);} /** * Intent Jump Info */private void Prepareintent () {//home Itmain = new Intent (this,mainactivity.class);//shopping cart Itcart = new Intent (This, Cartactivity.class);//My itmine = new Intent (this,mineactivity.class);} /* for Setupintent call * tag tag, load picture, toggle page * */private tabhost.tabspec buildtabspec (String tag, int reslabel, int resicon,final in Tent content) {return Mtabhost.newtabspec (tag). Setindicator (GetString (Reslabel), Getresources (). Getdrawable ( Resicon)). SetContent (content);} public static void Setcurrenttabbytag (String tab) {Mtabhost.setcurrenttabbytag (tab);} /** * Get tabhost */private void Setupintent () {mtabhost = Gettabhost (); Mtabhost.addtab (Buildtabspec (TAB_TAG_ANBIAOCHECK , R.string.securitycheck_btn_str,r.drawable.main_bottom_tab_home_normal, Itmain)); MTabHost.addTab (BuildTabSpec ( Tab_tag_settings,r.string.securitycheck_btn_str, R.drawable.main_bottom_tab_cart_focus, ItCart)); MTabHost.addTab (Buildtabspec (Tab_tag_download, R.string.securitycheck_btn_str,r.drawable.main_bottom_tab_personal_focus, ItMine ));} /* Click event listener */protected VoidInitview () {Llchannel1.setonclicklistener (this), Llchannel2.setonclicklistener (this); Llchannel3.setonclicklistener (this);} @Overridepublic boolean onKeyDown (int keycode,keyevent event) {if (keycode = = Keyevent.keycode_back && Event.getrepeatcount () = = 0) {Ivmain.performclick (); return true;} Return Super.onkeydown (KeyCode, event);} /* Response Unit Price event, change button picture */@Overridepublic void OnClick (View v) {if (Mcurtabid = = V.getid ()) {return;} int checkedid = V.getid (), Final Boolean o;if (Mcurtabid < checkedid) {o = true;} else {o = false;} if (o) {Mtabhost.getcurrentview (). Startanimation (left_out);} else {Mtabhost.getcurrentview (). Startanimation (Right_ Out);} Switch (checkedid) {//implements the effect of image switching similar to each sub-tabhost click, Case R.id.channel1:mtabhost.setcurrenttabbytag (tab_tag_ Anbiaocheck); Ivmine.setimageresource (R.drawable.main_bottom_tab_personal_focus); IvCart.setImageResource ( R.drawable.main_bottom_tab_cart_focus); Ivmain.setimageresource (r.drawable.main_bottom_tab_home_normal); break; Case r.id.channel2:mtabhost.seTcurrenttabbytag (tab_tag_settings); Ivmine.setimageresource (R.drawable.main_bottom_tab_personal_focus); Ivcart.setimageresource (R.drawable.main_bottom_tab_cart_normal); Ivmain.setimageresource (R.drawable.main_bottom _tab_home_focus); break;case R.id.channel3:mtabhost.setcurrenttabbytag (tab_tag_download); IvMine.setImageResource (r.drawable.main_bottom_tab_personal_normal); Ivcart.setimageresource (R.drawable.main_bottom_tab_cart_focus); Ivmain.setimageresource (r.drawable.main_bottom_tab_home_focus); break;default:break;} if (o) {Mtabhost.getcurrentview (). Startanimation (left_in);} else {Mtabhost.getcurrentview (). Startanimation (right_in);} Mcurtabid = Checkedid;}} </span>
The XML code of the page;
<span style= "font-family:kaiti_gb2312;" ><?xml version= "1.0" encoding= "Utf-8"? ><tabhost xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:id=" @android: Id/tabhost "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " android:background= "@color/black" android:orientation= "vertical" > <linearlayout android:lay Out_width= "Fill_parent" android:layout_height= "fill_parent" android:background= "@color/white" Android : orientation= "Vertical" > <framelayout android:id= "@android: Id/tabcontent" Android:layo Ut_width= "Fill_parent" android:layout_height= "Fill_parent" android:layout_weight= "1" > < ;/framelayout> <tabwidget android:id= "@android: Id/tabs" android:layout_width= "Fill_pare NT "android:layout_height=" Wrap_content "android:layout_weight=" 1 "Android:backgrouNd= "#000000" android:visibility= "Gone"/> <linearlayout android:layout_width= "Fill_pare NT "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true " android:background= "@color/whitesmoke" > <!--Home--<linearlayout an Droid:id= "@+id/channel1" style= "@style/main_tab_but_linear" > <imageview Android:id= "@+id/imageview1" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content" android:layout_margintop= "4DP" android:layout_weight= "1" android:src= "@drawable/main_bottom_tab_home_normal"/> </LinearLayout> <!--shopping Cart--<linearlayout android:id= "@+id/channel2" style= "@style/main _tab_but_linear "> <imageview android:id= "@+id/imageview2" android:layout_width= "Wrap_c Ontent "android:layout_height=" wrap_content "android:layout_margintop=" 4DP " android:layout_weight= "1" android:src= "@drawable/main_bottom_tab_cart_focus"/> </LinearLayout> <!--My--<linearlayout android:id= "@+id /channel3 "style=" @style/main_tab_but_linear "android:layout_width=" Fill_parent " android:layout_height= "Fill_parent" android:layout_weight= "1" android:orientation= "Verti Cal "> <imageview android:id=" @+id/imageview3 "Android:layout_w Idth= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margintop= "4 DP "Android:layout_weight= "1" android:src= "@drawable/main_bottom_tab_personal_focus"/> </l inearlayout> </LinearLayout> </LinearLayout> </TabHost></span>
Note:
The XML that was designed in the code to other jump pages. If you need to, you can create a blank XML page!
Although the necessary functions have been realized, the understanding of Tabhost has yet to be improved. The three functions used in Tabhost are also described in later articles. Please look forward to!
Tabhost for Android Menu switching