使用Tablayout和ViewPager2的特定样式选项卡 [英] Style specific tab using Tablayout and ViewPager2
问题描述
我正在寻找一种方法来独立设置标签样式,如下图所示:
I'm looking for a way how to style tabs independently like following image:
在这种情况下,我们具有不同的图标,背景色和tabIndicator颜色.看起来需要编程,因为选项卡是基于分页器适配器加载的.
In this case we have different icon, background color and tabIndicator color. Looks like needs to be programmatically because tabs are loaded base on pager adapter.
更新
申请@Chhatrasal Singh Bundela后回答
After applying @Chhatrasal Singh Bundela answer
选项卡上似乎有填充物,您知道如何使其占据所有选项卡区域.
Seams to have a padding in the tab, do you know how to make it take all tab area.
推荐答案
使用以下代码对我有用.
It works for me using the below code.
布局:item_tab.xml
layout: item_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/_30sdp">
<TextView
android:id="@+id/textView_tabTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:gravity="center"
android:text="Tab text here"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
public String [] tabTitles = new String [] {"Tab 1","Tab 2","Tab 3","Tab 4"};
public String[] tabTitles = new String[]{"Tab 1", "Tab 2", "Tab 3", "Tab 4"};
new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
View v = LayoutInflater.from(baseActivity).inflate(R.layout.item_tab, null, false);
TextView tv = v.findViewById(R.id.textView_tabTitle);
tv.setText(demoCollectionPagerAdapter.tabTitles[position]);
FrameLayout frameLayout = new FrameLayout(baseActivity);
frameLayout.addView(v);
tab.setCustomView(v);
}
}
).attach();
这篇关于使用Tablayout和ViewPager2的特定样式选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!