如何在tablayout中隐藏标签

How to hide a tab in tablayout

我有一个 tablayout 大约 9 tabs。我这样添加标签。

tabLayout.addTab(tabLayout.newTab());
TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tab.setText("TEXT");
tab.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_special_offer, 0, 0);
tabLayout.getTabAt(8).setCustomView(tab);

我们想要 disable 一些。我认为隐藏它比以各种方式进行验证以访问选项卡更有效。但我不知道如何隐藏它们。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout
    android:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:background="@color/colorFragmentBG">
    <RelativeLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/custom_tab_layout_height"
            android:layout_below="@+id/toolbar"
            android:background="?attr/colorPrimary"
            android:elevation="6dp"
            android:minHeight="?attr/actionBarSize"
            app:tabMode="scrollable"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/tab_layout"/>
     </RelativeLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>

如有任何帮助,我们将不胜感激。

首先你必须得到tabhost:

TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

然后使用此表达式,选择所需的选项卡:

tabHost.getTabWidget().getChildAt(8).setVisibility(View.GONE);

例如“8”将是第 9 个标签。

------------编辑------------

我假设你的 xml 是这样的:

<android.support.v4.app.FragmentTabHost
   android:id="@android:id/tabhost" …/>
   <LinearLayout …>
         <TabWidget
               android:id="@android:id/tabs" …/>
         <FrameLayout
               android:id="@android:id/tabcontent" …>
   </LinearLayout>
</TabHost>