"Android Essentials" tabwidget setting background and font

Source: Internet
Author: User

When using Tabhost to implement the bottom navigation bar, the bottom navigation bar of the three navigation buttons can not be customized in the layout file, such as setting the color of the click, font size and color, etc., here is a solution, is to customize in the code.

The idea is to add a page to the tabhost in the activity, and add the effect to the navigation buttons Tabwidget one by one (you must add a page before you can customize the button and add a paging to generate a button).

The following is the layout file Activity_main.xml, contains the tabhost, there are three only display the text of the pagination

<relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:id  = "@+id/container"  android:layout_width  =" match_parent " android:layout_height  =" wrap_content " android:orientation  =" vertical " tools:context  = "com.plan.MainActivity"  tools:ignore  = "Mergerootframe" ;     <tabhost  android:id  = "@android: Id/tabhost"  android:layout_width  = "fill_parent"  android:layout_height  = "fill_parent"  android:layout_alignparentleft  = android:layout_alignparenttop  =;         <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Fill_parent"android:orientation="vertical" >                                                <framelayoutandroid:id="@android: Id/tabcontent"android:layout_width ="Match_parent"android:layout_height="Fill_parent"android:layout_ Weight="0.8" >                                                                                <linearlayout  an Droid:id  = "@+id/tab1"  android:layout _width  = "match_parent"  android:layou T_height  = "match_parent"  android:ori Entation  =;                     <TextViewandroid:id= "@+id/textview1"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" Tab1 " />                                                                                                                 </linearlayout>                <linearlayout  an Droid:id  = "@+id/tab2"  android:layout _width  = "match_parent"  android:layou T_height  = "match_parent"  android:ori Entation  =;                     <TextViewandroid:id= "@+id/textview2"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" tab2 " />                                                                                                                 </linearlayout>                <linearlayout  an Droid:id  = "@+id/tab3"  android:layout _width  = "match_parent"  android:layou T_height  = "match_parent"  android:ori Entation  =;                     <TextViewandroid:id= "@+id/textview3"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" Tab3 " />                                                                                                                 </linearlayout>            </framelayout>            <tabwidgetandroid:id="@android: Id/tabs"android:layout_width=" Match_parent "android:layout_height=" Wrap_content " >                                                            </tabwidget>        </linearlayout>    </tabhost></relativelayout>

Here is the code in Mainactivity:

Packagecom. Aiplan_03;Import Android. App. Activitygroup;Import Android. Graphics. Color;Import Android. OS. Bundle;Import Android. Util. Log;Import Android. Widgets. Tabhost;Import Android. Widgets. Tabwidget;Import Android. Widgets. TextView;public class Mainactivity extends Activitygroup {tabhost mtabhost = null; Tabwidget mtabwidget = null; Tabwidget Controls@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Mtabhost = (tabhost) Findviewbyid (Android. R. ID. Tabhost);Mtabhost. Setup(This. Getlocalactivitymanager());Get navigation Button control Mtabwidget = Mtabhost. Gettabwidget();Add pagination1Mtabhost. AddTab(Mtabhost. Newtabspec("Button1"). SetContent(R. ID. Tab1). Setindicator("BTN1"));Add pagination2Mtabhost. AddTab(Mtabhost. Newtabspec("Button2"). SetContent(R. ID. Tab2). Setindicator("BTN2"));Add pagination3Mtabhost. AddTab(Mtabhost. Newtabspec("Button3"). SetContent(R. ID. Tab3). Setindicator("Btn3"));Log. D("Number of buttons", Integer. toString(Mtabwidget. Getchildcount()));Add special effects for (int i=0; I<mtabwidget.getchildcount (); i++) {Change font color TextView TV = (TextView) mtabwidget. Getchildat(i). Findviewbyid(Android. R. ID. Title);Tv. SetTextColor(Color. RGB(255,255,255));Setting the background map Mtabwidget. Getchildat(i). Setbackgroundresource(R. drawable. Tabwidget_selector);}    }}

Change the font of the navigation button to white, add a selector selector to the background of the navigation button, and the selector code:
Tabwidget_selector.xml, you need to put it under the Drawable folder

<?xml version= "1.0" encoding= "Utf-8"?  <selector  xmlns:android  = "http://schemas.android.com/apk/res/android" ;  <item  android:state_selected  =< Span class= "Hljs-value" > "false"  android:drawable  =  "@color/tabwidget_unselected" /> <item  android:state_selected  = " True " android:drawable  =" @color/tabwidget_ Selected "/>  </selector ;  

It used two colors, below is color.xml, need to put in the values folder

<resources xmlns:android="http://schemas.android.com/apk/res/android">    <color name="tabwidget_selected">#ff2222</color>    <color name="tabwidget_unselected">#000000</color></resources>

The last of the following:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Android Essentials" tabwidget setting background and font

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.