TabLayoutMediator 不保留 TabItem 属性 [英] TabLayoutMediator not preserving TabItem attributes
问题描述
我使用带有预设 TabItems 的 ViewPager2 和 TabLayout 进行了简单设置:
<代码>...<com.google.android.material.tabs.TabLayoutandroid:id="@+id/标签"android:layout_width="0dp"android:layout_height="64dp"应用程序:tabTextColor="@color/c0696D7"应用程序:layout_constraintEnd_toEndOf="父"app:layout_constraintStart_toEndOf="@+id/palettesToggle"应用程序:layout_constraintTop_toTopOf="父"app:tabBackground="@color/cEEEEEE"app:tabIndicatorColor="@color/cFFFFFF"应用程序:tabGravity =填充"应用程序:tabUnboundedRipple =真"app:tabIconTint="@color/palettes_icon_tint"应用程序:tabIndicatorGravity="拉伸"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.ViewPager2android:id="@+id/viewPager"android:layout_width="match_parent"android:layout_height="0dp"应用程序:layout_constraintBottom_toBottomOf="parent"应用程序:layout_constraintEnd_toEndOf="父"应用程序:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tabs"/>...
以及以下接线代码:
TabLayoutMediator(tabs, viewPager) { tab, position ->}.附()
以下设置忽略了 TabItem
图标属性,并且我看到了空标签.似乎 TabLayoutMediator
完全覆盖了定义的 xml 属性,我必须将这些属性重置为 TabConfigurationStrategy
回调的一部分.我错过了什么吗?
TabLayoutMediator.attach()
调用方法 tabLayout.removeAllTabs();
它删除所有标签,然后再次添加标签.
另请查看官方文档::><块引用>
创建此类的实例时,您必须提供TabLayoutMediator.TabConfigurationStrategy
的实现,您可以在其中设置选项卡的文本和/或执行任何样式您需要的选项卡.
类似于:
new TabLayoutMediator(tabs, viewpager, new TabLayoutMediator.TabConfigurationStrategy() {@Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {//配置你的标签...tab.setText(...);tab.setIcon(...);}}).附();
或:
TabLayoutMediator(tabs, viewpager,TabLayoutMediator.TabConfigurationStrategy { tab, position ->//配置标签..当(位置){0 ->{ tab.text = "..."}1 ->{ tab.text = "..."}}}).附()
I have a simple setup using ViewPager2 and TabLayout with preset 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" />
...
and the following wiring code:
TabLayoutMediator(tabs, viewPager) { tab, position ->
}.attach()
The following setup ignores the TabItem
icon attribute, and I see empty tabs. It seems that TabLayoutMediator
completely overrides the defined xml attributes and i must reset those attributes as part of the TabConfigurationStrategy
callback.
Am I missing something?
The method TabLayoutMediator.attach()
calls the method tabLayout.removeAllTabs();
which removes all tabs and then adds tabs again.
Check also the 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.
Something like:
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();
or:
TabLayoutMediator(tabs, viewpager,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
//Configure tabs..
when (position) {
0 -> { tab.text = "..."}
1 -> { tab.text = "..."}
}
}).attach()
这篇关于TabLayoutMediator 不保留 TabItem 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!