android - FragmentTabHost TabWidget tabStrip 无法自定义

标签 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);

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

mTabHost.getTabWidget().setStripEnabled(false);

tabStrip 仍然在那里,但是当我将其设置为 true 时,未选定项目的 strip 会变成灰色,我无法真正说出原因。

嗯,我的主要目标是更改该 tabStrip 的颜色或以其他方式将其完全删除,但由于这个问题,我无法确定应该如何做。我尝试使用膨胀 View ,但选择器不再工作,所以我无法再判断选项卡是否被选择。希望有人能帮助我了解如何改变它。但我真的希望有人可以帮助我如何更改 strip 颜色,因为这真的很烦人。

最佳答案

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

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

这对所有 child 来说都是有效的。虽然它解决了我的问题,但这不是最好的解决方案。 :)

关于android - FragmentTabHost TabWidget tabStrip 无法自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20538607/

相关文章:

android - 即使 fragment 被隐藏,FragmentTabHost 也会触发 onCreateView

android - 使用 fragment 方法在所有屏幕中显示选项卡选项

android - 在刷新或登录 google plus 之前,AdMob 不会显示横幅

java - Android AsyncTask/Service 在任务完成前被杀死

android - mediaplayer 在 android 中播放实时流音频的问题

Android SeekBar 设置进度值

android - 替换事务中的 fragment 时出现 fragment 后台错误?

android - 如何在带有 TabLayout 的 Fragment 中使用 CardView 行实现 RecyclerView

android - 将 AppCompatActivity 更改为 Fragment

Android,FragmentTabHost - 如何去除蓝色?