gpt4 book ai didi

android - TabLayoutMediator 不保留 TabItem 属性

转载 作者:行者123 更新时间:2023-12-03 13:26:09 30 4
gpt4 key购买 nike

我有一个使用 ViewPager2 和 TabLayout 和预设 TabItems 的简单设置:

...

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="0dp"
android:layout_height="64dp"
app:tabTextColor="@color/c0696D7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/palettesToggle"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="@color/cEEEEEE"
app:tabIndicatorColor="@color/cFFFFFF"
app:tabGravity="fill"
app:tabUnboundedRipple="true"
app:tabIconTint="@color/palettes_icon_tint"
app:tabIndicatorGravity="stretch"
app:tabMode="fixed">

<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_layers" />

<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_view" />

<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_properties" />

<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_blocks" />

<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_blocks" />

<com.google.android.material.tabs.TabItem android:icon="@drawable/palettes_settings" />

</com.google.android.material.tabs.TabLayout>

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabs" />

...

以及以下接线代码:
TabLayoutMediator(tabs, viewPager) { tab, position ->

}.attach()

以下设置忽略了 TabItem图标属性,我看到空选项卡。看来 TabLayoutMediator完全覆盖定义的 xml 属性,我必须将这些属性重置为 TabConfigurationStrategy 的一部分打回来。
我错过了什么吗?

最佳答案

方法TabLayoutMediator.attach() calls the method tabLayout.removeAllTabs();其中删除所有标签 然后再次添加标签。

另请查看 official doc :

When creating an instance of this class, you must supply an implementation of TabLayoutMediator.TabConfigurationStrategy in which you set the text of the tab, and/or perform any styling of the tabs that you require.



就像是:
new TabLayoutMediator(tabs, viewpager, new TabLayoutMediator.TabConfigurationStrategy() {
@Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//Configure your tabs...
tab.setText(...);
tab.setIcon(...);
}
}).attach();

或者:
 TabLayoutMediator(tabs, viewpager,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
//Configure tabs..
when (position) {
0 -> { tab.text = "..."}
1 -> { tab.text = "..."}
}
}).attach()

关于android - TabLayoutMediator 不保留 TabItem 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59054142/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com