【问题标题】:TabLayout.setTabTextColors() not working when trying to change text color尝试更改文本颜色时 TabLayout.setTabTextColors() 不起作用
【发布时间】:2015-09-29 09:09:12
【问题描述】:

我有一个可用的 TabLayout,并且我正在尝试在更改选项卡时动态更新选项卡文本颜色。为此,我在 TabLayout 上调用 setTabTextColors() 方法,如下所示:

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        tabLayout.setTabTextColors(newColorStateList);
    }

    (...)
});

由于某种原因,文本颜色没有更新。有谁知道如何动态更新标签文本颜色?

我正在使用设计支持库 v22.2.0。

【问题讨论】:

    标签: android android-support-library android-design-library android-tablayout


    【解决方案1】:

    TabLayout 有这样的方法 -

    setTabTextColors(int normalColor, int selectedColor)
    

    记住,int 不是颜色资源值,而是从十六进制解析的int

    例如:

    tabLayout.setTabTextColors(Color.parseColor("#D3D3D3"),Color.parseColor("#2196f3"))
    

    【讨论】:

    • "记住,int 不是颜色资源值,而是从 hex 解析的 int" 这真的很有帮助,谢谢
    【解决方案2】:

    另外,请确保不要使用单独的 xml 文件来设置选项卡的样式。像我这样的东西(custom_tab.xml):

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText(R.string.tab_response);
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.tab_bar_icon_response, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne); 
    

    【讨论】:

      【解决方案3】:

      它终于在设计支持库 22.2.1 中得到修复。

              tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                  tabLayout.setTabTextColors(getResources().getColor(R.color.normal), getResources().getColor(R.color.selected));
      
                  try {
                      // FIXME: 20.7.2015 WORKAROUND: https://code.google.com/p/android/issues/detail?id=175182 change indicator color
                      Field field = TabLayout.class.getDeclaredField("mTabStrip");
                      field.setAccessible(true);
                      Object value = field.get(tabLayout);
      
                      Method method = value.getClass().getDeclaredMethod("setSelectedIndicatorColor", Integer.TYPE);
                      method.setAccessible(true);
                      method.invoke(value, getResources().getColor(R.color.selected));
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                }
      
              ...
              }
      

      【讨论】:

        【解决方案4】:

        经过一番调查,TabLayout 中的文本视图似乎在创建后没有更新颜色。

        我想出的解决方案是遍历 TabLayout 的子视图并直接更新它们的颜色。

        public static void setChildTextViewsColor(ViewGroup viewGroup, ColorStateList colorStateList) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
        
                if (child instanceof ViewGroup) {
                    setChildTextViewsColor((ViewGroup) child, colorStateList);
                } else if (child instanceof TextView) {
                    TextView textView = (TextView) child;
                    textView.setTextColor(colorStateList);
                }
            }
        }
        

        然后,在 OnTabSelectedListener 中:

            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    setChildTextViewsColor(tabLayout, newColorStateList);
                }
        
                (...)
            });
        

        【讨论】:

          猜你喜欢
          • 2017-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-17
          • 2016-10-04
          • 2022-08-23
          • 2015-11-01
          • 1970-01-01
          相关资源
          最近更新 更多