为TabLayout中当前选项卡移除选择器线。

32
我正在使用带有3个选项卡的TabLayout。我自定义了我的选项卡视图,因此需要删除选项卡下面的以下线条(截图不来自我的应用程序):

enter image description here

没有使用TabHost或TabWidget,因此不能使用setStripEnabled(false)。将背景设置为透明也没有改变任何东西。

这是我的xml:

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:padding="4dip"
    android:layout_above="@+id/bottomcontent3"
    android:gravity="center_horizontal"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/comtabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:tabPaddingStart="0dp"
        app:tabPaddingEnd="0dp"
        app:tabMode="fixed"
        app:tabGravity="fill"
        android:background="@android:color/white" />

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/compager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

</android.support.design.widget.AppBarLayout>
我找到的所有答案都是使用TabHostTabWidget。在我的情况下,我正在使用一个TabLayout和三个Tab。如何在这种情况下删除这行?非常感谢。 编辑:由于某些原因,我的代码中无法解析来自TabLayout的某些方法。以下是我使用的Java代码:
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.comtabs);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);


        // add tabs
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());

        RelativeLayout layout1 = (RelativeLayout) inflater.inflate(R.layout.communitytablayoutleft, container, false);
        RelativeLayout layout2 = (RelativeLayout) inflater.inflate(R.layout.communitytablayout, container, false);
        RelativeLayout layout3 = (RelativeLayout) inflater.inflate(R.layout.communitytablayoutright, container, false);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);



        ViewPager pager = (ViewPager) view.findViewById(R.id.compager);
        CommunityPagerFragment adapter = new CommunityPagerFragment(getChildFragmentManager());

        pager.setAdapter(adapter);
        tabLayout.setupWithViewPager(pager);
        tabLayout.setOnTabSelectedListener(this);


        ((TextView)layout1.findViewById(R.id.tabtext)).setText(tabs[0]);
        ((TextView)layout2.findViewById(R.id.tabtext)).setText(tabs[1]);
        ((TextView)layout3.findViewById(R.id.tabtext)).setText(tabs[2]);



        tabLayout.getTabAt(0).setCustomView(layout1);
        tabLayout.getTabAt(1).setCustomView(layout2);
        tabLayout.getTabAt(2).setCustomView(layout3);
        //tabLayout.set

        return view;

以及其重要性:

import android.support.design.widget.TabLayout;
6个回答

67

正如两个答案所建议的那样,关键是tabIndicatorHeight属性。

然而,由于某些原因,API中的方法无法解决。在这种情况下,您必须直接从xml中修复它,方法如下:

        app:tabIndicatorHeight="0dp"

在你的<android.support.design.widget.TabLayout>布局中。

举个例子:

<android.support.design.widget.TabLayout
    android:id="@+id/comtabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:tabIndicatorHeight="0dp"
    app:tabPaddingStart="0dp"
    app:tabPaddingEnd="0dp"
    app:tabMode="fixed"
    app:tabGravity="fill"
    android:background="@android:color/white" />

19

最近我也遇到了同样的问题。我发现通过更改高度xml属性为0dp就解决了我的问题。

app:tabIndicatorHeight="0dp"

或者

同样地,我认为将指示符的颜色属性更改为与选项卡背景相同也可以解决问题。我尚未测试过这个解决方案,但我认为它应该也可以奏效。

app:tabIndicatorColor="#9cc8df"

第一种选择更好,而且对我有用。

虽然没有真正禁用指示器,但这些解决方法都能解决问题,其中任何一个都不错。


app:tabIndicatorColor="@android:color/transparent" 这个简单多了。 - D. B.

9

我认为最好的解决方案是将选项卡指示器设置为null。

app:tabIndicator="@null"


5

如果您想要使用 TabLayout,您需要使用以下代码 - "tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);"

TabLayout tabLayout = (TabLayout) fragmentView.findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);

4

我无法解决这些方法。让我编辑我的主要帖子,附上Java代码,也许会有所帮助。 - Virthuss
我找到了一种通过XML来实现的方法。我会给你点赞 :) 谢谢! - Virthuss

4

您可以将选项卡指示器高度设置为0来“移除”它,例如使用: tabLayout.setSelectedTabIndicatorHeight(0);


你是否正在使用 Android Support Design Library 中的 TabLayout? - jomartigcal
1
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 然后 tabLayout.setSelectedTabIndicatorHeight(0); - jomartigcal
这正是我所做的。仍然无法解决。让我将我的代码推送到主帖中。 - Virthuss
我找到了一种通过 XML 应用它的方法。我会给你点赞 :) 谢谢! - Virthuss
setSelectedTabIndicatorHeight 在支持库 28.0.0 中已被弃用 :( 不过仍然可以使用。 - 0101100101

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接