【问题标题】:Selected custom tab text color in TabLayoutTabLayout 中选定的自定义选项卡文本颜色
【发布时间】:2017-10-21 19:17:28
【问题描述】:

我正在尝试创建自定义选项卡布局,因为我需要在 TextView 旁边设置徽章计数器。我已将 id 设置为@android:id/text1,正如文档中提到的那样。

当我的自定义选项卡被选中时,TextView 颜色不会自动更改。如何以正确和干净的方式实现它?

正确选择默认选项卡:

错误选择的自定义选项卡(文本为灰色但应为白色):

代码

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);
}

布局

<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>

编辑

答案:

tab.setCustomView(R.layout.tab_proposed_rewards);
ColorStateList textColor = tabLayout.getTabTextColors();
TextView textView = (TextView) tab.getCustomView().findViewById(android.R.id.text1);
textView.setTextColor(textColor);

【问题讨论】:

  • 答案在于here,第二个答案。使用您的styles.xml,这样您就可以使用style 标记通过xml 创建视图,而不是以编程方式创建它。就个人而言,我认为这是“更清洁”的方式。
  • @DillonBurton 自定义选项卡布局不起作用,但无论如何感谢您的帮助。我找到了不同的解决方案。
  • @AleksanderMielczarek 你是怎么做到的。我也陷入了困境。

标签: android android-layout tabs android-tablayout


【解决方案1】:

其实还是用选择器比较好。

这是一个使用 Kotlin 的示例和带有 tabLayout 的最新 viewPager2(基于 Google 的示例 here):

        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>

badge_background.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>

【讨论】:

    【解决方案2】:

    您可以以编程方式执行此操作。

    以编程方式更改代码中选定选项卡的颜色。您可以使用setTabTextColors (int normalColor, int selectedColor)

    然后申请

    yourTabLayout.setTabTextColors (Color.White, Color.Black);
    

    希望这能解决你的问题,更多信息可以在link找到

    你的情况

    TabHost tabHost = getTabHost();
    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
            { 
                TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
                tv.setTextColor(Color.parseColor("#ffffff"));
            } 
            TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
            tv.setTextColor(Color.parseColor("#000000"))
    

    试试这个,它会改变内部文本视图的颜色

    【讨论】:

    • 感谢 tabTextColors 的提示。我从 TabLayout 获得默认的 textColors 并将它们应用于自定义 TextView。请参阅已编辑的问题。
    • @AleksanderMielczarek 看到更新的答案,它有代码来改变 textView 文本的颜色
    猜你喜欢
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多