【Android基礎篇】TabWidget設定背景和字型,androidtabwidget
在使用TabHost實現底部導覽列時,底部導覽列的三個導覽按鈕無法在布局檔案裡進行定製,比如設定點擊時的顏色、字型的大小及顏色等,這裡提供了一個解決辦法,就是在代碼裡進行定製。
思路是在Activity裡給TabHost添加了分頁後,在給導覽列TabWidget的導覽按鈕逐個添加特效(必須先添加分頁,然後才能定製按鈕,添加了一個分頁,才會產生一個按鈕)。
下面是布局檔案activity_main.xml,包含了TabHost,裡面有三個只顯示了文字的分頁
<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="true" android:layout_alignParentTop="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="0.8" > <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab1" /> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab2" /> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab3" /> </LinearLayout> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> </LinearLayout> </TabHost></RelativeLayout>
下面是MainActivity裡的代碼:
package com.aiplan_03;import android.app.ActivityGroup;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.widget.TabHost;import android.widget.TabWidget;import android.widget.TextView;public class MainActivity extends ActivityGroup { TabHost mTabHost = null; TabWidget mTabWidget = null; //TabWidget控制項 @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()); //擷取導覽按鈕控制項 mTabWidget = mTabHost.getTabWidget(); //添加分頁1 mTabHost.addTab(mTabHost.newTabSpec("button1").setContent( R.id.tab1).setIndicator("btn1")); //添加分頁2 mTabHost.addTab(mTabHost.newTabSpec("button2").setContent( R.id.tab2).setIndicator("btn2")); //添加分頁3 mTabHost.addTab(mTabHost.newTabSpec("button3").setContent( R.id.tab3).setIndicator("btn3")); Log.d("按鈕數",Integer.toString(mTabWidget.getChildCount())); //逐個按鈕添加特效 for(int i=0;i<mTabWidget.getChildCount();i++){ //換字型顏色 TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title); tv.setTextColor(Color.rgb(255, 255, 255)); //設定背景圖 mTabWidget.getChildAt(i).setBackgroundResource( R.drawable.tabwidget_selector); } }}
把導覽按鈕的字型換成了白色,給導覽按鈕的背景添加了一個selector選取器,下面是選取器代碼:
tabwidget_selector.xml,需放到drawable檔案夾下
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="false" android:drawable="@color/tabwidget_unselected" /> <item android:state_selected="true" android:drawable="@color/tabwidget_selected" /> </selector>
裡面用到了兩個顏色,下面是color.xml,需放到values檔案夾下
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <color name="tabwidget_selected">#ff2222</color> <color name="tabwidget_unselected">#000000</color></resources>
最後的如下:
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。