Android notes: A simple method to customize tabhost through RadioGroup/RadioButton

Source: Internet
Author: User

XML code:

<RelativeLayout xmlns: android =" http://schemas.android.com/apk/res/android "Xmlns: tools =" http://schemas.android.com/tools "Android: layout_width =" match_parent "android: layout_height =" match_parent "tools: context = ". mainActivity "> <TabHost android: id =" @ android: id/tabhost "android: layout_width =" match_parent "android: layout_height =" match_parent "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TabWidget android: id = "@ android: id/tabs "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: visibility =" gone "> </TabWidget> <FrameLayout android: id =" @ android: id/tabcontent "android: layout_width =" match_parent "android: layout_height =" 0dp "android: layout_weight =" 1 "> </FrameLayout> <RadioGroup android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_gravity = "bottom" android: orientation = "horizontal"> <RadioButton android: id = "@ + id/maintab_radio_tab1" style = "@ style/tab_button" android: checked = "true" android: text = "Homepage" android: background = "# ff00ff00"/> <RadioButton android: id = "@ + id/maintab_radio_tab2" style = "@ style/tab_button" android: text = "pagination" android: background = "# ff00ff00"/> <RadioButton android: id = "@ + id/maintab_radio_tab3" style = "@ style/tab_button" android: text = "Settings" android: background = "# ff00ff00"/> </RadioGroup> </LinearLayout> </TabHost> </RelativeLayout>


style="@ Style/tab_button" file:

<! -- RadioButton style at the bottom of the home page --> <style name = "tab_button"> <item name = "android: layout_width"> fill_parent </item> <item name = "android: layout_height "> wrap_content </item> <item name =" android: ellipsize "> marquee </item> <item name =" android: button "> @ null </item> <item name =" android: paddingTop "> 1dp </item> <item name =" android: layout_weight "> 1.0 </item> <item name =" android: gravity "> center_horizontal </item> <item name =" android: singleLine "> true </item> <item name =" android: drawablePadding "> 1.0dip </item> </style>


Layout Structure: Hide the TabWidget and use the RadioButton option in the Remove button check box)


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/105T92S9-0.png "title =" qq2013020.38.png "/>



Java code:

Public class MainActivity extends TabActivity implements OnClickListener {private RadioButton mBtn_tab1; private RadioButton listener; private TabHost mTabHost; @ Override protected void onCreate (Bundle listener) super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init (); initTab () ;}// initialization interface private void init () {mBtn_tab1 = (RadioButton) findViewById (R. id. maintab_radio_tab1); mBtn_tab2 = (RadioButton) findViewById (R. id. maintab_radio_tab2); mBtn_tab3 = (RadioButton) findViewById (R. id. maintab_radio_tab3); merge (this); mBtn_tab3.setOnClickListener (this);} // initialize the private void initTab () {mTabHost = getTabHost (); mTabHost. addTab (mTabHost. newTabSpec ("tab1 "). setIndicator ("tab1 "). setContent (new Intent (this, Tab1Activity. class); mTabHost. addTab (mTabHost. newTabSpec ("tab2 "). setIndicator ("tab2 "). setContent (new Intent (this, Tab2Activity. class); mTabHost. addTab (mTabHost. newTabSpec ("tab3 "). setIndicator ("tab3 "). setContent (new Intent (this, Tab1Activity. class);} @ Override public void onClick (View v) {switch (v. getId () {case R. id. maintab_radio_tab1: mTabHost. setCurrentTabByTag ("tab1"); break; case R. id. maintab_radio_tab2: mTabHost. setCurrentTabByTag ("tab2"); break; case R. id. maintab_radio_tab3: mTabHost. setCurrentTabByTag ("tab3"); break; default: break ;}}}








This article is from the "no trace in the sky but I fly over" blog. For more information, contact the author!

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.