Custom TabHost text and background for Android Development (source code sharing)

Source: Internet
Author: User

You can use TabHost to switch between different la s on a single screen. The tabhost interface provided by the system is simple. How can we optimize custom modification?

MainActivity source code

Package com. dream. ledong; import android. app. tabActivity; import android. content. intent; import android. graphics. color; import android. OS. bundle; import android. view. gravity; import android. widget. relativeLayout; import android. widget. tabHost; import android. widget. tabHost. onTabChangeListener; import android. widget. tabWidget; import android. widget. textView; import com. example. client. r; @ SuppressWarnings ("deprecation") public class itemList extends TabActivity {@ Overridepublic void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. itemlist); final TabHost tabHost = getTabHost (); Intent remoteIntent = new Intent (itemList. this, item1.class); TabHost. tabSpec remoteTabSpec = tabHost. newTabSpec ("remote"); remoteTabSpec. setIndicator ("motion recommendation"); remoteTabSpec. setContent (remoteIntent); tabHost. addTab (remoteTabSpec); Intent localIntent = new Intent (itemList. this, item2.class); TabHost. tabSpec localTabSpec = tabHost. newTabSpec ("local"); localTabSpec. setIndicator (""); localTabSpec. setContent (localIntent); tabHost. addTab (localTabSpec); Intent localIntent2 = new Intent (itemList. this, item2.class); TabHost. tabSpec localTabSpec2 = tabHost. newTabSpec ("a"); localTabSpec2.setIndicator ("competitive atmosphere"); localTabSpec2.setContent (localIntent2); tabHost. addTab (localTabSpec2); updateTabStyle (tabHost); // when a Tab is selected, the background style tabHost is updated. listener (new OnTabChangeListener () {@ Overridepublic void onTabChanged (String tabId) {updateTabStyle (tabHost) ;}} private void updateTabStyle (final TabHost mTabHost) {TabWidget tabWidget = mTabHost. getTabWidget (); tabWidget. setRightStripDrawable (R. drawable. list_item_divide_operate); tabWidget. setLeftStripDrawable (R. drawable. list_item_divide_operate); for (int I = 0; I <tabWidget. getChildCount (); I ++) {RelativeLayout tabView = (RelativeLayout) mTabHost. getTabWidget (). getChildAt (I); TextView text = (TextView) tabWidget. getChildAt (I ). findViewById (android. r. id. title); text. setTextSize (15); RelativeLayout. layoutParams params = (RelativeLayout. layoutParams) text. getLayoutParams (); params. width = RelativeLayout. layoutParams. MATCH_PARENT; params. height = RelativeLayout. layoutParams. MATCH_PARENT; text. setLayoutParams (params); text. setGravity (Gravity. CENTER); if (mTabHost. getCurrentTab () = I) {// select tabView. setBackgroundColor (Color. parseColor ("# 8DB6CD"); text. setTextColor (this. getResources (). getColorStateList (android. r. color. black);} else {// The tabView is not selected. setBackgroundColor (Color. parseColor ("# ffffff"); text. setTextColor (this. getResources (). getColorStateList (android. r. color. darker_gray ));}}}}


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.