我正在尝试创建自定义选项卡布局,因为我需要在旁边设置徽章计数器TextView
。我已将 id 设置为@android:id/text1
正如文档中提到的。
当选择我的自定义选项卡时,TextView 颜色不会自动更改。如何以正确且干净的方式实现它?
正确选择默认选项卡:
错误选择了自定义选项卡(文本为灰色,但应为白色):
Code
PagerAdapter adapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
TabLayout.Tab tab = tabLayout.getTabAt(2);
if (tab != null) {
tab.setCustomView(R.layout.tab_proposed_rewards);
}
Layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="center"
android:textAppearance="@style/TextAppearance.Design.Tab"/>
<TextView
android:id="@+id/indicator"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/background_indicator"
android:gravity="center"
android:lines="1"/>
</LinearLayout>
Edit
Answer:
tab.setCustomView(R.layout.tab_proposed_rewards);
ColorStateList textColor = tabLayout.getTabTextColors();
TextView textView = (TextView) tab.getCustomView().findViewById(android.R.id.text1);
textView.setTextColor(textColor);
实际上,最好使用选择器。
这是一个使用 Kotlin 和带有 tabLayout 的最新 viewPager2 的示例(基于 Google 的示例)here https://github.com/android/views-widgets-samples):
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
val tabView = LayoutInflater.from(this).inflate(R.layout.tab_with_badge, tabLayout, false)
tabView.textView.text = "item$position"
tabView.badgeTextView.text = position.toString()
tab.customView = tabView
}.attach()
tab_with_badge.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="@color/tab_color_selector" tools:text="@tools:sample/lorem" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/badgeTextView" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/badge_background" tools:text="12" />
</LinearLayout>
tab_color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#f00" android:state_pressed="true" />
<item android:color="#0f0" android:state_selected="true" />
<item android:color="#00f" />
</selector>
徽章_背景.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:left="4dp"
android:right="4dp" />
<solid android:color="@color/tab_color_selector" />
<corners android:radius="8dp" />
</shape>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)