2

这对我来说真的很困惑,因为每当我设置 FragmentTabHost 的 StripEnabled 时,它并没有按照我想要的方式进行。

从这里开始是 FragmentTabHost 的代码:

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        Bundle b = new Bundle();

        b.putString("0", "tab1");
        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(null,getResources().getDrawable(R.drawable.selector_tab1)),
                Fragment1.class, b);

        b = new Bundle();
        b.putString("1", "tab2");
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(null, getResources().getDrawable(R.drawable.selector_tab2)),
                Fragment2.class, b);

和 XML 文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

现在,当我添加这些代码行时,它工作得很好:

mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);

它成功隐藏了选项卡上的所有分隔符,但是当我使用这行代码禁用 tabstrip 时:

mTabHost.getTabWidget().setStripEnabled(false);

tabStrip 仍然在那里,但是当我将其设置为 true 时,未选中项目的条带变成灰色,我真的不知道为什么。

好吧,我的主要目标是更改该 tabStrip 的颜色或以其他方式将其完全删除,但是对于这个问题,我真的不能确定我应该怎么做。我尝试使用膨胀视图,但选择器不再起作用,所以我无法判断选项卡是否被选中。希望有人可以帮助我解决这个问题。但我真的希望有人能帮助我改变条带颜色,因为这真的很烦人。

4

1 回答 1

3

我已经通过使用选择器更改背景资源来更改它。我使用了 9 个补丁图像来确保一切都适合。这是添加所有选项卡后的代码:

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_selector);

只是为了所有的孩子,它有效。虽然它解决了我的问题,但不是最好的解决方案。:)

于 2013-12-18T11:42:51.157 回答