Evg*_*urg 12 android android-tablayout material-components-android android-viewpager2
我使用带有预设 TabItems 的 ViewPager2 和 TabLayout 进行了简单设置:
...
<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" />
...
Run Code Online (Sandbox Code Playgroud)
以及以下接线代码:
TabLayoutMediator(tabs, viewPager) { tab, position ->
}.attach()
Run Code Online (Sandbox Code Playgroud)
以下设置忽略了TabItemicon 属性,我看到了空标签。似乎TabLayoutMediator完全覆盖了定义的 xml 属性,我必须将这些属性重置为TabConfigurationStrategy回调的一部分。我错过了什么吗?
Gab*_*tti 11
该方法TabLayoutMediator.attach() 调用该方法 tabLayout.removeAllTabs();其去除所有标签,然后再添加标签。
还要检查官方文档:
创建此类的实例时,您必须提供一个实现
TabLayoutMediator.TabConfigurationStrategy,您可以在其中设置选项卡的文本和/或执行所需的选项卡的任何样式。
就像是:
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();
Run Code Online (Sandbox Code Playgroud)
或者:
TabLayoutMediator(tabs, viewpager,
TabLayoutMediator.TabConfigurationStrategy { tab, position ->
//Configure tabs..
when (position) {
0 -> { tab.text = "..."}
1 -> { tab.text = "..."}
}
}).attach()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1676 次 |
| 最近记录: |