【问题标题】:How to hide a tab in tablayout如何在标签布局中隐藏标签
【发布时间】:2016-04-13 17:34:11
【问题描述】:

我有一个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>

任何帮助将不胜感激。

【问题讨论】:

    标签: android tabs android-tablayout


    【解决方案1】:

    首先你必须得到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>
    

    【讨论】:

    • 是 tabhost 的一个内置组件,我需要在我的布局中添加它
    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多