Android: Custom tab Style

Source: Internet
Author: User
1. create four tab styles for 9patch. For details, refer to the default Android resource.

Tab_unselected.9.png tab_selected.9.pngtab_press.9.pngtab_focus.9.png

These four resources represent the four statuses of the tab.

2. Define the selector style of the tab (called tab_indicator.xml) and put it in the drawable folder,CodeAs follows:

 

<? XML version = "1.0" encoding = "UTF-8" ?>
< Selector Xmlns: Android = "Http://schemas.android.com/apk/res/android" >
<! -- Non focused states -->
< Item Android: state_focused = "False" Android: state_selected = "False" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_unselected"   />
< Item Android: state_focused = "False" Android: state_selected = "True" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_selected"   />
<! -- Focused states -->
< Item Android: state_focused = "True" Android: state_selected = "False" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_focus"   />
< Item Android: state_focused = "True" Android: state_selected = "True" Android: state_pressed = "False" Android: drawable = "@ Drawable/tab_focus"   />
<! -- Pressed -->
< Item Android: state_pressed = "True" Android: drawable = "@ Drawable/tab_press"   />
</ Selector >

3. Compile the indicator layout file (also called tab_indicator.xml) and put it in the layout folder. The Code is as follows:

 

<? XML version = "1.0" encoding = "UTF-8" ?>
< Relativelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "0dip"
Android: layout_height = "64dip"
Android: layout_weight = "1"
Android: layout_marginleft = "-3dip"
Android: layout_marginright = "-3dip"
Android: Orientation = "Vertical"
Android: Background = "@ Drawable/tab_indicator" >
< Imageview Android: ID = "@ + ID/icon"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: layout_centerhorizontal = "True"
/>
< Textview Android: ID = "@ + ID/Title"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: layout_alignparentbottom = "True"
Android: layout_centerhorizontal = "True"
Style = "? Android: ATTR/tabwidgetstyle" Mce_style = "? Android: ATTR/tabwidgetstyle"
/>

4. Next we will use our own tab style in tabactivity:

 

// Obtain the tabwidget first
Mtabhost = gettabhost ();
Linearlayout LL = (linearlayout) mtabhost. getchildat (0 );
Tabwidget Tw = (tabwidget) ll. getchildat (0 );

Then, use the code similar to the following to create tabspec.

 

Relativelayout tabindicator1 = (relativelayout) layoutinflater. From (this). Inflate (R. layout. tab_indicator, TW, false );
Textview tvtab1 = (textview) tabindicator1.getchildat (1 );
Tvtab1.settext ("tab1 ");
Mtabhot = mtabhost. newtabspec ("tab_1 ")
. Setindicator (tabindicator1)
. Setcontent (contentintent );

 

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.