FragmentTabHost TabWidget tabStrip无法自定义

时间:2013-12-12 08:39:19

标签: android android-fragments fragment-tab-host

这对我来说真的很混乱,因为每当我设置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);

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

mTabHost.getTabWidget().setStripEnabled(false);

tabStrip仍在那里,但是当我将其设置为true时,未选中项目的条带变为灰色,我无法确定原因。

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

1 个答案:

答案 0 :(得分:3)

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

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

对所有孩子来说都是如此,它有效。虽然它解决了我的问题,但不是最佳解决方案。 :)

相关问题