Android Development Review notes-TabHost component (2) (bottom menu navigation), Android tabhost

Source: Internet
Author: User

Android Development Review notes-TabHost component (2) (bottom menu navigation), Android tabhost

In the previous article "android Development Review notes -- TabHost component (1) (implementing the bottom menu navigation)", we mentioned using custom View (ImageView + TextView) to set the style of a bottom menu.

Here we will add a more flexible method to hide the TabWidget and use (RadioGroup + RadioButton) instead, and use the listener to jump to the Activity by clicking and clicking the listener.

 

Before explaining this, add the following points:

1. When we obtain the TabHost instance object, we can set the current interface content (Activity) in two ways)

You can find the following APIs:

These two methods use the page identifier (int, start page is 0) and page tag (String) to set the current display page.

We can use these two methods to jump to the corresponding page when listening to and clicking Radio.

 

2. In the XML layout file, the sub-tag RadioButton of the tag RadioGroup has several notes:

1. android: button = "@ null" is used to hide the radio button style provided by the system.

2. android: drawableTop = "@ drawable/tab_icon1" is the icon used to set the radio button (the system provides the icon on the left by default, which can be set on it)

3. android: drawablePadding = "3dp" is used to set the distance between the button icon and the button label.

 

3. Set TabWidget to hide: android: visibility = "gone"

 

Why is this method more flexible?

My friends who have read the previous article should know that I used a custom code layout in the previous article. I need to get the XML layout object in the JAVA code and then set the style, now, with RadioGroup + RadioButton, we can set all the styles in the XML layout file, which is easier to maintain, in JAVA code, we can focus more on code implementation of business logic.

 

Now let's go to the code. Because it is almost the same as the previous example, I will not post the repeated parts, but only the core part of the Code (the comments are 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 14 public class MainActivity extends ActivityGroup {15 16 private TabHost tabHost; // declare a TabHost object 17 18 private RadioGroup radioGroup; // declare a RadioGroup Object 19 20 // resource file 21 private Class activitys [] = {TabActivity1.class, TabActivity2.class, TabActivity3.class, TabActivity4.class, TabActivity5.class }; // jump to the Activity22 private String title [] = {"Homepage", "Search", "set", "topic", "more "}; // set the menu title 23 private int image [] = {R. drawable. tab_icon1, R. drawable. tab_icon2, R. drawable. tab_icon3, R. drawable. tab_icon4, R. drawable. tab_icon5,}; // set menu 24 25 @ Override26 protected void onCreate (Bundle savedInstanceState) {27 super. onCreate (savedInstanceState); 28 setContentView (R. layout. activity_main); 29 initTabView (); // initialize the tab 30 31} 32 33 private void initTabView () {34 // instantiate tabhost35 this. tabHost = (TabHost) findViewById (R. id. mytabhost); 36 // because ActivityGroup is inherited, you must add this parameter to the setup method. If TabActivity is inherited, 37 tabHost can be omitted. setup (this. getLocalActivityManager (); 38 39 // create tag 40 for (int I = 0; I <activitys. length; I ++) {41/* Because RadioGroup is used, the style has been set in xml, therefore, you do not need to customize view 42. // instantiate a view as the layout of the tab. 43 View = view. inflate (this, R. layout. tab_layout, null); 44 45 // set imageview46 ImageView imageView = (ImageView) view. findViewById (R. id. image); 47 imageView. setImageDrawable (getResources (). getDrawable (image [I]); 48 // set textview49 TextView textView = (TextView) view. findViewById (R. id. title); 50 textView. setText (title [I]); */51 52 // set the 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]). setIndicator (title [I]). setContent (intent); 57 58 // Add to tab 59 tabHost. addTab (spec); 60} 61 62 radioGroup = (RadioGroup) findViewById (R. id. radiogroup); 63 radioGroup. setOnCheckedChangeListener (new OnCheckedChangeListener () {64 65 @ Override66 public void onCheckedChanged (RadioGroup group, int checkedId) {67 switch (checkedId) {68 69 case R. id. radio1: 70 // tabHost. setCurrentTab (int id); use the current page number to jump to activity71 // tabHost. setCurrentTabByTag (String tag); use the current tag to jump to activity72 tabHost. setCurrentTabByTag (title [0]); 73 break; 74 case R. id. radio2: 75 tabHost. setCurrentTabByTag (title [1]); 76 break; 77 case R. id. radio3: 78 tabHost. setCurrentTabByTag (title [2]); 79 break; 80 case R. id. radio4: 81 tabHost. setCurrentTabByTag (title [3]); 82 break; 83 case R. id. radio5: 84 tabHost. setCurrentTabByTag (title [4]); 85 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 android: layout_width = "fill_parent" 4 android: layout_height = "fill_parent"> 5 6 <! -- A layout manager is required --> 7 8 <RelativeLayout 9 android: layout_width = "fill_parent" 10 android: layout_height = "fill_parent"> 11 12 <! -- 13 because TabHost is inherited from FrameLayout, A FrameLaytout layout (Content Page) is required. id 14 must be tabcontent 15 --> 16 17 <FrameLayout 18 android: id = "@ android: id/tabcontent "19 android: layout_width =" fill_parent "20 android: layout_height =" fill_parent "> 21 </FrameLayout> 22 23 <! -- TabWidget must be a tag used to store the tab, and the id must be tabs --> 24 25 <TabWidget 26 android: id = "@ android: id/tabs" 27 android: layout_width = "fill_parent" 28 android: layout_height = "wrap_content" 29 android: layout_alignParentBottom = "true" 30 android: background = "@ drawable/tab_widget_background" 31 android: visibility = "gone"> 32 </TabWidget> 33 34 <RadioGroup 35 android: id = "@ + id/radiogroup" 36 android: layout_width = "fill_parent" 37 android: layout_height = "wrap_content" 38 android: layout_alignParentBottom = "true" 39 android: background = "@ drawable/tab_widget_background" 40 android: orientation = "horizontal" 41 android: padding = "3dp"> 42 43 <RadioButton 44 android: id = "@ + id/radio1" 45 android: layout_width = "wrap_content" 46 android: layout_height = "wrap_content" 47 android: layout_weight = "1" 48 android: background = "@ drawable/tab_selector" 49 android: button = "@ null" 50 android: drawablePadding = "3dp" 51 android: drawableTop = "@ drawable/tab_icon1" 52 android: gravity = "center_horizontal" 53 android: text = "Homepage" 54 android: textColor = "@ android: color/white" 55 android: textSize = "10sp"/> 56 57 <RadioButton 58 android: id = "@ + id/radio2" 59 android: layout_width = "wrap_content" 60 android: layout_height = "wrap_content" 61 android: layout_weight = "1" 62 android: background = "@ drawable/tab_selector" 63 android: button = "@ null" 64 android: drawablePadding = "3dp" 65 android: drawableTop = "@ drawable/tab_icon2" 66 android: gravity = "center_horizontal" 67 android: text = "Search" 68 android: textColor = "@ android: color/white" 69 android: textSize = "10sp"/> 70 71 <RadioButton 72 android: id = "@ + id/radio3" 73 android: layout_width = "wrap_content" 74 android: layout_height = "wrap_content" 75 android: layout_weight = "1" 76 android: background = "@ drawable/tab_selector" 77 android: button = "@ null" 78 android: drawablePadding = "3dp" 79 android: drawableTop = "@ drawable/tab_icon3" 80 android: gravity = "center_horizontal" 81 android: text = "Settings" 82 android: textColor = "@ android: color/white" 83 android: textSize = "10sp"/> 84 85 <RadioButton 86 android: id = "@ + id/radio4" 87 android: layout_width = "wrap_content" 88 android: layout_height = "wrap_content" 89 android: layout_weight = "1" 90 android: background = "@ drawable/tab_selector" 91 android: button = "@ null" 92 android: drawablePadding = "3dp" 93 android: drawableTop = "@ drawable/tab_icon4" 94 android: gravity = "center_horizontal" 95 android: text = "topic" 96 android: textColor = "@ android: color/white" 97 android: textSize = "10sp"/> 98 99 <RadioButton100 android: id = "@ + id/radio5" 101 android: layout_width = "wrap_content" 102 android: layout_height = "wrap_content" 103 android: layout_weight = "1" 104 android: background = "@ drawable/tab_selector" 105 android: button = "@ null" 106 android: drawablePadding = "3dp" 107 android: drawableTop = "@ drawable/tab_icon5" 108 android: gravity = "center_horizontal" 109 android: text = "more" 110 android: textColor = "@ android: color/white" 111 android: textSize = "10sp"/> 112 </RadioGroup> 113 </RelativeLayout> 114 115 </TabHost>

 


This is an android tabhost. You can click the "other" component in the ta

Hey, this tabhost is an example everywhere. You should have a pdf or an api.

Which Android can use Gallery to build a TABHOST tab page? You need to drag a tab or display a pretty tab on the tab page below.

What does LZ want is the function of switching the TAB label forward or backward? This can be achieved by responding to touch screen events. If you want to change the TabHost style, you need android 1.6 or later. You can customize the TabHost style by layout. If it is less than 1.6, You need to rewrite the TabHost source code, which is a little more difficult.
 

Related Article

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.