更改选项卡背景颜色并删除选项卡之间的分隔线

时间:2011-06-01 07:46:51

标签: android tabs

我想在我的应用程序中显示标签,但默认情况下,在标签之间的android中有像这样的分隔线

                            Tab1 | Tab2 | Tab3 |

但我想显示像这样的标签

                            Tab1 Tab2 Tab3

所以我想删除两个标签之间的分隔线,默认情况下标签背景颜色是灰色。所以我想把它变成黑色。

请告诉我们如何删除两个标签之间的分隔线并更改标签的背景颜色。

提前致谢。

最诚挚的问候。

3 个答案:

答案 0 :(得分:61)

使用:

tabHost.getTabWidget().setDividerDrawable(null);

删除分隔线。

答案 1 :(得分:24)

我在ICS中遇到了问题,其中分隔符可见。除以下情况外,没有一种解决方案有效。

<TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:gravity="bottom"
            android:layout_alignParentBottom="true"
            android:fadingEdge="none"
            android:showDividers="none" >
        </TabWidget>

关键字是android:showDividers="none"

答案 2 :(得分:10)

使用此方法和布局为选项卡使用您自己的布局。要删除分隔符,只需将背景9patch图形替换为您自己的。

public static View prepareTabView(Context context, String text, Drawable background) {
    View view = LayoutInflater.from(context).inflate(R.layout.fake_native_tab, null);
    TextView tv = (TextView) view.findViewById(R.id.fakeNativeTabTextView);
    tv.setText(text);
    view.setBackgroundDrawable(background);
    return view;
}

fake_native_tab.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fakeNativeTabLayout" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center"
android:orientation="vertical" android:background="@drawable/default_tab_background">
<!--
       You can even define an Icon here (dont forget to set a custom icon in your code for each Tab):
    <ImageView android:id="@+id/fakeNativeTabImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:src="@drawable/icon" />
-->
    <TextView android:id="@+id/fakeNativeTabTextView"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="@color/tab_text_color" android:textSize="@dimen/text_size_tiny"
    android:text="Tab" android:ellipsize="marquee" />

</LinearLayout>

用法(在TabActivity内):

/* Create Tabs */
// reusable Tab Spec
TabHost.TabSpec spec;
Intent tabIntent;
tabHost = getTabHost();
Resources res = getResources();

// Tab 1:
tabIntent = new Intent().setClass(this, Favorite.class);
    spec = tabHost.newTabSpec(TAB_SOMETAB).setIndicator(
            prepareTabView(this, (String) getText(R.string.tab_favorite), res
                    .getDrawable(R.drawable.tab_favorite_background), 0)).setContent(tabIntent);
tabHost.addTab(spec);



// Tab 2:
tabIntent = new Intent().setClass(this, History.class);
spec = tabHost.newTabSpec(TAB_SOMEOTHERTAB).setIndicator(
            prepareTabView(this, (String) getText(R.string.tab_history), res
                    .getDrawable(R.drawable.tab_favorite_background), 0)).setContent(tabIntent);
tabHost.addTab(spec);