About how to change the color background color of the Tab font for Android TabHost

Source: Internet
Author: User

[Java]
Recently I am working on a tablet ordering system. I want to use TabHost, which is not very easy to write. I have prepared and shared it with you. The default effect is as follows:

Recently I am working on a tablet ordering system. I want to use TabHost, which is not very easy to write. I have prepared and shared it with you. The default effect is as follows:

 
 

Effect after Switch

 


First, the layout file in the layout folder, the Code is as follows:

<TabHost xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ android: id/tabhost"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>


<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
Android: background = "@ drawable/category_bg"
Android: padding = "0dp">


<TabWidget
Android: id = "@ android: id/tabs"
Android: layout_width = "wrap_content"
Android: layout_height = "40dp"/>


<FrameLayout
Android: id = "@ android: id/tabcontent"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "@ drawable/context_bg"
Android: padding = "0dp"/>
</LinearLayout>


</TabHost>

 

 

Then the java file,


[Java]
Package com. dzdc. activity;
 
 
Import android. app. TabActivity;
Import android. content. Intent;
Import android. graphics. Typeface;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. Window;
Import android. view. WindowManager;
Import android. widget. TabHost;
Import android. widget. TabHost. OnTabChangeListener;
Import android. widget. TextView;
 
 
Import com. dzdc. R;
 
 
@ SuppressWarnings ("deprecation ")
Public class IndexActivity extends TabActivity {
Private String [] tabMenu = {"hot dishes", "cold dishes", "seafood", "Sichuan dishes", "wine drinks", "card dishes "};
Private Intent intent0, intent1, intent2, intent3, intent4, intent5;
Private Intent [] intents = {intent0, intent1, intent2, intent3, intent4,
Intent5 };
Private TabHost. TabSpec tabSpec0, tabSpec1, tabSpec2, tabSpec3, tabSpec4,
TabSpec5;
Private TabHost. TabSpec [] tabSpecs = {tabSpec0, tabSpec1, tabSpec2,
TabSpec3, tabSpec4, tabSpec5 };
 
 
Private TabHost tabHost = null;
 
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title bar.
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (R. layout. index );
 
 
TabHost = getTabHost ();
 
 
For (int I = 0; I <tabMenu. length; I ++ ){
Intents [I] = new Intent ();
Intents [I]. setClass (this, IndexContentActivity. class );
 
 
TabSpecs [I] = tabHost. newTabSpec (tabMenu [I]);
TabSpecs [I]. setIndicator (tabMenu [I]); // set text
TabSpecs [I]. setContent (intents [I]); // you can specify the content of this page.
 
 
TabHost. addTab (tabSpecs [I]); // Add the content of this page to Tabhost
}
 
 
TabHost. setCurrentTabByTag (tabMenu [0]); // you can specify the default label that is displayed when you enable it for the first time,

UpdateTab (tabHost); // initialize the Tab color and font color.

TabHost. setOnTabChangedListener (new OnTabChangedListener (); // select the listener
 
 
}
 
 
Class OnTabChangedListener implements OnTabChangeListener {
 
 
@ Override
Public void onTabChanged (String tabId ){
TabHost. setCurrentTabByTag (tabId );
System. out. println ("tabid" + tabId );
System. out. println ("curreny after:" + tabHost. getCurrentTabTag ());
UpdateTab (tabHost );
}
}
 
 
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0 ){
System. exit (0 );
Return false;
} Else if (keyCode = KeyEvent. KEYCODE_MENU
& Event. getRepeatCount () = 0 ){
Return true; // if true is returned, the default setting menu is not displayed.
}
 
 
Return false;
}

/**
* Update the Tab color and font color.
* @ Param tabHost
*/
Private void updateTab (final TabHost tabHost ){
For (int I = 0; I <tabHost. getTabWidget (). getChildCount (); I ++ ){
View view = tabHost. getTabWidget (). getChildAt (I );
TextView TV = (TextView) tabHost. getTabWidget (). getChildAt (I). findViewById (android. R. id. title );
TV. setTextSize (16 );
TV. setTypeface (Typeface. SERIF, 2); // set the font and Style
If (tabHost. getCurrentTab () = I) {// select
View. setBackgroundDrawable (getResources (). getDrawable (R. drawable. category_current); // The selected background
TV. setTextColor (this. getResources (). getColorStateList (
Android. R. color. black ));
} Else {// not selected
View. setBackgroundDrawable (getResources (). getDrawable (R. drawable. category_bg); // unselected background
TV. setTextColor (this. getResources (). getColorStateList (
Android. R. color. white ));
}
}
}
}

Package com. dzdc. activity;


Import android. app. TabActivity;
Import android. content. Intent;
Import android. graphics. Typeface;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. Window;
Import android. view. WindowManager;
Import android. widget. TabHost;
Import android. widget. TabHost. OnTabChangeListener;
Import android. widget. TextView;


Import com. dzdc. R;


@ SuppressWarnings ("deprecation ")
Public class IndexActivity extends TabActivity {
Private String [] tabMenu = {"hot dishes", "cold dishes", "seafood", "Sichuan dishes", "wine drinks", "card dishes "};
Private Intent intent0, intent1, intent2, intent3, intent4, intent5;
Private Intent [] intents = {intent0, intent1, intent2, intent3, intent4,
Intent5 };
Private TabHost. TabSpec tabSpec0, tabSpec1, tabSpec2, tabSpec3, tabSpec4,
TabSpec5;
Private TabHost. TabSpec [] tabSpecs = {tabSpec0, tabSpec1, tabSpec2,
TabSpec3, tabSpec4, tabSpec5 };


Private TabHost tabHost = null;


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE); // remove the title bar.
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (R. layout. index );


TabHost = getTabHost ();


For (int I = 0; I <tabMenu. length; I ++ ){
Intents [I] = new Intent ();
Intents [I]. setClass (this, IndexContentActivity. class );


TabSpecs [I] = tabHost. newTabSpec (tabMenu [I]);
TabSpecs [I]. setIndicator (tabMenu [I]); // set text
TabSpecs [I]. setContent (intents [I]); // you can specify the content of this page.


TabHost. addTab (tabSpecs [I]); // Add the content of this page to Tabhost
}


TabHost. setCurrentTabByTag (tabMenu [0]); // you can specify the default label that is displayed when you enable it for the first time,

UpdateTab (tabHost); // initialize the Tab color and font color.

TabHost. setOnTabChangedListener (new OnTabChangedListener (); // select the listener


}


Class OnTabChangedListener implements OnTabChangeListener {


@ Override
Public void onTabChanged (String tabId ){
TabHost. setCurrentTabByTag (tabId );
System. out. println ("tabid" + tabId );
System. out. println ("curreny after:" + tabHost. getCurrentTabTag ());
UpdateTab (tabHost );
}
}


@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0 ){
System. exit (0 );
Return false;
} Else if (keyCode = KeyEvent. KEYCODE_MENU
& Event. getRepeatCount () = 0 ){
Return true; // if true is returned, the default setting menu is not displayed.
}


Return false;
}
 
/**
* Update the Tab color and font color.
* @ Param tabHost
*/
Private void updateTab (final TabHost tabHost ){
For (int I = 0; I <tabHost. getTabWidget (). getChildCount (); I ++ ){
View view = tabHost. getTabWidget (). getChildAt (I );
TextView TV = (TextView) tabHost. getTabWidget (). getChildAt (I). findViewById (android. R. id. title );
TV. setTextSize (16 );
TV. setTypeface (Typeface. SERIF, 2); // set the font and Style
If (tabHost. getCurrentTab () = I) {// select
View. setBackgroundDrawable (getResources (). getDrawable (R. drawable. category_current); // The selected background
TV. setTextColor (this. getResources (). getColorStateList (
Android. R. color. black ));
} Else {// not selected
View. setBackgroundDrawable (getResources (). getDrawable (R. drawable. category_bg); // unselected background
TV. setTextColor (this. getResources (). getColorStateList (
Android. R. color. white ));
}
}
}
}


 

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.