【发布时间】:2018-10-24 11:07:32
【问题描述】:
我有一个TabLayout,我想自定义如下:
- 如果未选择选项卡项,则仅显示一个图标
- 如果选择了选项卡项,则应显示图标和文本
我已经实现了一个OnTabSelectedListener,并在未选择选项卡时将文本设置为987654325 @ @。当我运行时,“未选择”选项卡上的文本被删除,但该选项卡仍处于活动状态,如下图所示:
代码:
tabs.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener{
override fun onTabReselected(p0: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
// If I remove this the problem is resolved.
tab?.text = null
}
override fun onTabSelected(tab: TabLayout.Tab?) {
tab?.text = "Selected"
}
})
xml
<com.google.android.material.tabs.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabInlineLabel="true"
app:tabMode="scrollable"
app:tabGravity="fill">
<com.google.android.material.tabs.TabItem
style="@style/Widget.MaterialComponents.TabLayout.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_attach_money_black_24dp"
android:text="Selected" />
<com.google.android.material.tabs.TabItem
style="@style/Widget.MaterialComponents.TabLayout.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_attach_money_black_24dp" />
<com.google.android.material.tabs.TabItem
style="@style/Widget.MaterialComponents.TabLayout.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_attach_money_black_24dp"
/>
<com.google.android.material.tabs.TabItem
style="@style/Widget.MaterialComponents.TabLayout.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_attach_money_black_24dp" />
<com.google.android.material.tabs.TabItem
style="@style/Widget.MaterialComponents.TabLayout.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_attach_money_black_24dp"
/>
</com.google.android.material.tabs.TabLayout>
我已尽我所能寻找解决方案,但无济于事。
【问题讨论】:
标签: android material-design material-components