Android TabHost dynamically modifies the icon or the title

Source: Internet
Author: User
AbHost mTabHost;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the TabHost object mTabHost = getTabHost ();/* Add a label for TabHost * // create a newTabSpec (newTabSpec) // set its label and Icon (setIndicator) // set the content (setContent) mTabHost. addTab (mTabHost. newTabSpec ("tab_1 "). setIndicator ("TAB 1", getResour Ces (). getDrawable (R. drawable. img1 )). setContent (R. id. textview1); mTabHost. addTab (mTabHost. newTabSpec ("tab_2 "). setIndicator ("TAB 2", getResources (). getDrawable (R. drawable. img2 )). setContent (R. id. textview2); mTabHost. addTab (mTabHost. newTabSpec ("tab_3 "). setIndicator ("TAB 3", getResources (). getDrawable (R. drawable. img3 )). setContent (R. id. textview3); // set the background color of TabHost mTabHost. setBackgroundColor (Color. Argb (150, 22, 70,150); // sets the background image resource mTabHost for TabHost. setBackgroundResource (R. drawable. bg0); // set which label is currently displayed mTabHost. setCurrentTab (0); // tag switching event processing, setOnTabChangedListener mTabHost. setOnTabChangedListener (new OnTabChangeListener () {@ Override public void onTabChanged (String tabId) {}});} transfer to core issues. 1. Change the title icon of TabHost. First, we should get the Layout View and the corresponding control. The icon should be ImageView, and the text should be TextView. The Code is as follows: View mView = mTabHost. getTabWidget (). getChildAt (0); // 0 indicates the first TabImageView imageView = (ImageView) mView. findViewById (Android. r. id. icon); // obtain the control imageViewimageView. setImageDrawable (getResources (). getDrawable (R. drawable. img3); // change the image view = (imageView) mTabHost. getTabWidget (). getChildAt (1 ). findViewById (Android. r. id. icon); imageView. setImageDrawable (getResources (). getDrawable (R. drawable. img 2); imageView = (ImageView) mTabHost. getTabWidget (). getChildAt (2 ). findViewById (Android. r. id. icon); imageView. setImageDrawable (getResources (). getDrawable (R. drawable. img1); 2. change the title of TabHost. TextView textview = (TextView) mTabHost. getTabWidget (). getChildAt (0 ). findViewById (Android. r. id. title); textview. setTextSize (1); textview = (TextView) mTabHost. getTabWidget (). getChildAt (1 ). findViewById (Android. r. id. title); textview. setTextSize (2); textview = (TextView) mTabHost. getTabWidget (). getChildAt (2 ). findViewById (Android. r. id. title); textview. setTextSize (3); in fact, after obtaining the View, we can also set the title Color, Font, font size, and so on.

 

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.