您需要定义TabWidget
的android:background
属性。例如:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/tab_backgrnd" />
</RelativeLayout>
凡tab_backgrnd
可以是任何drawable。
首先定义的布局,由于Android本身并没有标签。下面是Android的标签布局是这样的:
<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" />
</RelativeLayout>
你可以把你想要这个布局什么,而不仅仅是一个ImageView
和一个TextView
。为了使标签页改变按压/选中时,设置StateListDrawable
作为根布局的背景和其它元素的背景,其中所述标签包含(在上面的例子ImageView
和TextView
)。
然后,你膨胀此布局,并设置为标签指示器:
TabHost tabHost = getTabHost();
TabHost.TabSpec tabSpec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
View tabView;
LayoutInflater inflater = getLayoutInflater();
intent = new Intent().setClass(this, YourActivity1.class);
tabView = inflater.inflate(R.layout.tab_layout, tabHost.getTabWidget(), false);
tabSpec = tabHost.newTabSpec(TAG_TAB_1).setIndicator(tabView).setContent(intent);
tabHost.addTab(tabSpec);
// Do the same for the other tabs
...
(由于Android不利用android:id="@+id/icon"
和android:id="@+id/title"
IDS)可在设置图标和每个选项卡的编程的文本或用户还可以定义单独的每个选项卡的布局。
有一个很好的android教程,但它看起来像他们把它从他们的网站。 – matt5784