Custom tabhost text and backgrounds for Android development (source code sharing)

Source: Internet
Author: User

The use of tabhost can be in a screen between different layout of the switch, and the system comes with a simple tabhost interface, we should be how to customize the modification of the optimization?

Source code for Mainactivity

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 (); I Ntent 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 ("ball friend Chi"); 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, update the background style Tabhost.setontabchangedlistener (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 = (Rel Ativelayout) 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 ();p arams.width = Relativelayout.layoutparams.match_parent;params.height = RelativeLayout.LayoutParams.MATCH_PARENT; Text.setlayoutparams (params); Text.setgravity (Gravity.center), if (mtabhost.getcurrenttab () = = i) {//Check Tabview.setbackgroundcolor ( Color.parsecolor ("#8DB6CD")); Text.settextcolor (This.getresources (). Getcolorstatelist (Android. R.color.black));} else {//unchecked Tabview.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.