[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 ));
}
}
}
}