Android TabHost solves the following white lines

Source: Internet
Author: User

Previously, pagination was implemented using GridView and ActivityGroup. because TabHost is not easy to use, there is another reason for poor level. if you want to help, rewrite view to solve any problem.
I will not explain much about the use of TabHost. There are many examples on the Internet, or you can download my Demo for viewing. However, it is hard to write a statement first.
Code snippet:
[Java]
Public class MyActivity extends TabActivity {
Private TabWidget tabWidget;
 
/** Called when the activity is first created .*/
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
Resources res = getResources (); // Resource object to get Drawables
Final TabHost tabHost = getTabHost (); // The activity TabHost
TabHost. TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
 
// Create an Intent to launch an Activity for the tab (to be reused)
Intent = new Intent (). setClass (this, ArtistsActivity. class );
 
// Initialize a TabSpec for each tab and add it to the TabHost
Spec = tabHost
. NewTabSpec ("artists ")
. SetIndicator ("Artists ",
Res. getDrawable (R. drawable. ic_tab_artists ))
. SetContent (intent );
TabHost. addTab (spec );
 
// Do the same for the other tabs
Intent = new Intent (). setClass (this, AlbumsActivity. class );
Spec = tabHost
. NewTabSpec ("albums ")
. SetIndicator ("Albums ",
Res. getDrawable (R. drawable. ic_tab_artists ))
. SetContent (intent );
TabHost. addTab (spec );
 
Intent = new Intent (). setClass (this, SongsActivity. class );
Spec = tabHost
. NewTabSpec ("songs ")
. SetIndicator ("Songs ",
Res. getDrawable (R. drawable. ic_tab_artists ))
. SetContent (intent );
TabHost. addTab (spec );
TabHost. setCurrentTab (2 );
View v;
TabWidget = tabHost. getTabWidget ();
 
For (int I = 0; I <tabWidget. getChildCount (); I ++ ){
// Obtain the tabview item
V = tabWidget. getChildAt (I );
// Set the tab background color
V. setBackgroundResource (android. R. color. white );
// Obtain the textview control (white by default)
TextView textView = (TextView) v. findViewById (android. R. id. title );
TextView. setTextColor (Color. BLACK );
// The default option is to be processed.
If (tabHost. getCurrentTab () = I)
V. setBackgroundResource (R. drawable. renren_sdk_pay_repair_btn );
}
// Tabchanged listener
TabHost. setOnTabChangedListener (new OnTabChangeListener (){
// The value in newTabSpec is displayed in tabId.
@ Override
Public void onTabChanged (String tabId ){
// Initialize all view backgrounds.
For (int I = 0; I <tabHost. getTabWidget (). getChildCount (); I ++ ){
View v = tabHost. getTabWidget (). getChildAt (I );
// Set the tab background color
V. setBackgroundResource (android. R. color. white );
// Process the selected
If (tabHost. getCurrentTab () = I ){
V. setBackgroundResource (R. drawable. renren_sdk_pay_repair_btn );
}
 
}
}
});
 
}
}
Author: jj120522

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.