Turn-tabhost Component (ii) (Implement bottom menu navigation)

Source: Internet
Author: User

Http://www.cnblogs.com/lichenwei/p/3975095.html

The above article "Android Development Review Notes--tabhost component (i) (Achieve bottom menu navigation)" mentioned in the use of Custom view (Imageview+textview) to set a bottom menu style

Here, add a more flexible approach, you can hide tabwidget, use (Radiogroup+radiobutton) to replace, and use the listener's way to achieve monitoring click to jump activity.

Before you explain, add a few points:

1, when we get the instance object of Tabhost, we can set the current interface content (Activity) in 2 ways.

Looking at the next API, we can find:

These 2 methods, respectively, use the page marker (int, start page 0) and page label (String) to reach the setting of the current display page

We can use these 2 methods to jump to the corresponding page when the radio is tapped.

2, in the layout file XML, the label Radiogroup Sub-label RadioButton There are a few points to note:

1, android:button= "@null" This is used to hide the system itself provides a radio button style

2, android:drawabletop= "@drawable/tab_icon1" This is used to set the radio button icon (the system is provided by default on the left, this can be set on the top)

3, android:drawablepadding= "3DP" This is used to set the button icon and the distance of the button label

3, need to set Tabwidget to hide: android:visibility= "Gone"

Why is it more flexible to use this method?

The friends who have read my last article should know that in the last article I used the custom code layout, I need to get the XML layout object in Java code, then set the style, and now use Radiogroup+radiobutton, We can set all the styles in the XML layout file, which is easier to maintain, and in the Java code we can focus more on the code implementation of the business logic.

OK, next to the code, because the same as the previous example, the repetition of the part I do not post, only the core of the code (comments are very full)

 1 package com.example.tabhosttest; 2 3 Import Android.app.ActivityGroup; 4 Import android.content.Intent; 5 Import Android.os.Bundle; 6 Import Android.view.View; 7 Import Android.widget.ImageView; 8 Import Android.widget.RadioGroup; 9 Import android.widget.radiogroup.oncheckedchangelistener;10 Import android.widget.tabhost;11 Import ANDROID.WIDGET.TABHOST.TABSPEC;12 Import android.widget.textview;13 public class Mainactivity extends Activitygroup     {19 Private Tabhost tabhost;//declares a Tabhost object, Radiogroup radiogroup;//declares a Radiogroup object 20 Resource Files Private Class activitys[]={tabactivity1.class,tabactivity2.class,tabactivity3.class,tabactivity4.class,t abactivity5.class};//Jump Activity22 Private String title[]={"Home", "Search", "Settings", "Themes", "more"};//the title of the settings menu in the new int image []={r.drawable.tab_icon1,r.drawable.tab_icon2,r.drawable.tab_icon3,r.drawable.tab_icon4,r.drawable.tab_icon5, };//Setup Menu @Override26 protected void onCreate (Bundle saVedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); 29 Inittabview ();//Initialize tab tag}32 private void Inittabview () {34//instantiation Tabhost35 Thi         S.tabhost= (tabhost) Findviewbyid (r.id.mytabhost); 36//Because Activitygroup is inherited, you need to include this parameter in the Setup method, and if you inherit tabactivity you can omit 37 Tabhost.setup (This.getlocalactivitymanager ()); 38 39//Create label for (int i=0;i<activitys.le             ngth;i++) {41/* due to the use of Radiogroup, the style has been set in XML, so there is no need to customize view 42//Instantiate a View as tab tab Layout 43             View View=view.inflate (this, r.layout.tab_layout, null); 44 45//Set imageview46 ImageView imageview= (ImageView) View.findviewbyid (r.id.image); Imageview.setimagedrawable (Getresources (). GE Tdrawable (Image[i])); 48//Set textview49 TextView textview= (TextView) View.findviewbyid (r.id.title); Textview.sEttext (Title[i]);    */51 52//Set Jump activity53 Intent intent=new Intent (this, activitys[i]); 54 55//Load the View object and set the jump activity56 tabspec spec=tabhost.newtabspec (Title[i]). Setindicato         R (Title[i]). SetContent (intent); 57 58//Add to tab Tabhost.addtab (spec); 60}61 radiogroup= (Radiogroup) Findviewbyid (R.id.radiogroup), Radiogroup.setoncheckedchangelistener (NE  W Oncheckedchangelistener () {@Override66 public void oncheckedchanged (Radiogroup                     Group, int checkedid) {checkedid switch {r.id.radio1:70 Tabhost.setcurrenttab (int id); Use the current page to jump activity71//tabhost.setcurrenttabbytag (String tag);                 Use the current tab to jump activity72 Tabhost.setcurrenttabbytag (title[0]); break;74 CaSe r.id.radio2:75 tabhost.setcurrenttabbytag (title[1]); break;77                 Case r.id.radio3:78 Tabhost.setcurrenttabbytag (title[2]); break;80                 Case r.id.radio4:81 Tabhost.setcurrenttabbytag (title[3]); break;83                 Case r.id.radio5:84 Tabhost.setcurrenttabbytag (title[4]); break;86 }87 88}89}); 90 91}92 93 94}

  1 <tabhost xmlns:android= "http://schemas.android.com/apk/res/android" 2 android:id= "@+id/mytabhost" 3 Andro     Id:layout_width= "Fill_parent" 4 android:layout_height= "Fill_parent" > 5 6 <!--need a layout manager-7 8   <relativelayout 9 android:layout_width= "fill_parent" android:layout_height= "fill_parent" > 11 <!--13 since Tabhost is inherited from Framelayout, a framelaytout layout (content page) is required, and ID 14 must be tabcontent 15- <framelayout-android:id= "@android: Id/tabcontent" Android:lay         Out_width= "Fill_parent" android:layout_height= "fill_parent" > </FrameLayout> 22 23 <!--Tabwidget must be labeled to hold the tab tag, and the ID must be tabs--and <tabwidget android:id= "@android: ID             /tabs "android:layout_width=" fill_parent "android:layout_height=" wrap_content "29 Android:layout_alignpaRentbottom= "true" android:background= "@drawable/tab_widget_background" to android:visibility= "G             One "> </TabWidget> <radiogroup android:id=" @+id/radiogroup "36 Android:layout_width= "Fill_parent" PNs android:layout_height= "Wrap_content" android:layou T_alignparentbottom= "true" android:background= "@drawable/tab_widget_background"-Android:orie ntation= "Horizontal" android:padding= "3DP" > <radiobutton Andro Id:id= "@+id/radio1" android:layout_width= "Wrap_content" android:layout_height=                 Content "android:layout_weight=" 1 "android:background=" @drawable/tab_selector "49 android:button= "@null" android:drawablepadding= "3DP" Wuyi Android:drawablet op= "@drawable/tab_icon1" Android:gravity= "Center_horizontal" android:text= "Home" Android:te                 Xtcolor= "@android: Color/white" android:textsize= "10sp"/>-<radiobutton 58 Android:id= "@+id/radio2" android:layout_width= "Wrap_content" Android:lay out_height= "Wrap_content" android:layout_weight= "1" android:background= "@drawable/ta                 B_selector "android:button=" @null "android:drawablepadding=" 3DP "65 android:drawabletop= "@drawable/tab_icon2" android:gravity= "Center_horizontal" Androi d:text= "Search" android:textcolor= "@android: Color/white" android:textsize= "10sp"/> 7 0 <radiobutton android:id= "@+id/radio3" android:layout_width= "Wra           P_content "74      android:layout_height= "Wrap_content" android:layout_weight= "1" Android:backgro                  und= "@drawable/tab_selector" android:button= "@null" android:drawablepadding= "3DP" 79                 android:drawabletop= "@drawable/tab_icon3" android:gravity= "Center_horizontal" 81 android:text= "Set" android:textcolor= "@android: Color/white" Android:textsi Ze= "10sp"/> <radiobutton android:id= "@+id/radio4" Android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_weig                 ht= "1" android:background= "@drawable/tab_selector" android:button= "@null" 92 android:drawablepadding= "3DP" android:drawabletop= "@drawable/tab_icon4" 94 and Roid:gravity= "Center_horIzontal "android:text=" theme "android:textcolor=" @android: Color/white "97                 Android:textsize= "10sp"/> 98 <radiobutton100 android:id= "@+id/radio5" 101 Android:layout_width= "Wrap_content" 102 android:layout_height= "Wrap_content" 103 an droid:layout_weight= "1" 104 android:background= "@drawable/tab_selector" android:button= "                 @null "106 android:drawablepadding=" 3DP "107 android:drawabletop=" @drawable/tab_icon5 "108 Android:gravity= "Center_horizontal" 109 android:text= "more" Android:textcolo R= "@android: Color/white" 111 android:textsize= "10SP"/>112 </radiogroup>113 </relat ivelayout>114 </TabHost>

Turn-tabhost Component (ii) (Implement bottom menu navigation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.