1.你需要定义TabWidget
'sandroid: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。
2 & 3.首先,您像 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>
您可以在此布局中放入您想要的任何东西,而不仅仅是 oneImageView
和 one 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 一样),或者您可以为每个选项卡定义单独的布局。