为 TabLayout 移除当前 Tab 下的选择器行
Remove the selector line under current Tab for a TabLayout
我正在使用带有 3 个选项卡的 TabLayout。我自定义了选项卡的视图,因此,我需要删除选项卡下的以下行(屏幕截图不是来自我的应用程序):
我 不 使用 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>
我找到的所有答案都使用 TabHost
、TabWidget
。在我的例子中,我使用了一个 TabLayout
和三个 Tab
。
在这种情况下如何删除此行?非常感谢。
EDIT 由于某些原因,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;
好像有API喜欢;
void setSelectedTabIndicatorColor(int color)
void setSelectedTabIndicatorHeight(int height)
您可以将标签指示器高度设置为 0 到 "remove" 它,例如使用:
tabLayout.setSelectedTabIndicatorHeight(0);
正如两个答案所暗示的那样,关键是 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" />
对于 TabLayout 你必须使用这个 - "tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);"
TabLayout tabLayout = (TabLayout) fragmentView.findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);
我最近也遇到了同样的问题。我遇到了高度 xml 属性并将其更改为 0dp 对我有用。
app:tabIndicatorHeight="0dp"
或
同样我觉得把指标的颜色属性改成和tabs的背景一样应该也行。我没有测试过这个解决方案,但我认为它也应该有效。
app:tabIndicatorColor="#9cc8df"
第一个选项更好,对我有用。
None 这些与实际禁用指示器一样好,但不管怎样都能解决问题。
我认为最好的解决方案是将选项卡指示器设置为空。
app:tabIndicator="@null"
我正在使用带有 3 个选项卡的 TabLayout。我自定义了选项卡的视图,因此,我需要删除选项卡下的以下行(屏幕截图不是来自我的应用程序):
我 不 使用 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>
我找到的所有答案都使用 TabHost
、TabWidget
。在我的例子中,我使用了一个 TabLayout
和三个 Tab
。
在这种情况下如何删除此行?非常感谢。
EDIT 由于某些原因,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;
好像有API喜欢;
void setSelectedTabIndicatorColor(int color)
void setSelectedTabIndicatorHeight(int height)
您可以将标签指示器高度设置为 0 到 "remove" 它,例如使用:
tabLayout.setSelectedTabIndicatorHeight(0);
正如两个答案所暗示的那样,关键是 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" />
对于 TabLayout 你必须使用这个 - "tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);"
TabLayout tabLayout = (TabLayout) fragmentView.findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorColor(Color.TRANSPARENT);
我最近也遇到了同样的问题。我遇到了高度 xml 属性并将其更改为 0dp 对我有用。
app:tabIndicatorHeight="0dp"
或
同样我觉得把指标的颜色属性改成和tabs的背景一样应该也行。我没有测试过这个解决方案,但我认为它也应该有效。
app:tabIndicatorColor="#9cc8df"
第一个选项更好,对我有用。
None 这些与实际禁用指示器一样好,但不管怎样都能解决问题。
我认为最好的解决方案是将选项卡指示器设置为空。
app:tabIndicator="@null"