【问题标题】:Tablayout's text color does not change when setting the selected item in code在代码中设置选中项时,Tablayout 的文本颜色不会改变
【发布时间】:2016-08-20 16:29:18
【问题描述】:

我正在尝试让 tabLayout 更改它的选定选项卡。

viewPager = (ViewPager) findViewById(R.id.configuration_sheet_pager);
    tabLayout = (TabLayout) findViewById(R.id.configuration_sheet_tabs);
    view.setSelectedTabIndicatorColor(selectedColor);
    view.setTabTextColors(normallColor, selectedColor);
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);

要选择一个标签:

//Does not work; indicator moves but text color is not affected
viewPager.setCurrentItem(change.value, change.animated);
//works as expected
tabLayout.setScrollPosition(change.value,0f,true);
viewPager.setCurrentItem(change.value);

我正在使用 Android Design 支持库 23.1.1。我发现了一个错误吗?

【问题讨论】:

    标签: android android-tablayout


    【解决方案1】:

    如果您希望TabLayout 在您选择 tabLayout 时更改颜色,xml 应该是这样的:

      <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/color_primary"
            app:tabGravity="fill"
            app:tabIndicatorColor="#f32"
            app:tabIndicatorHeight="4dp"
            app:tabMode="fixed"/>
    

    然后调用

    tabLayout.setupWithViewPager(viewPager);
    

    执行以下操作:

    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
    
                @Override
                public void onPageSelected(int position) {
                    tabLayout.getTabAt(position).select();
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });
    

    【讨论】:

      【解决方案2】:

      在 Material TabLayout 中添加这个:

      app:tabTextColor="@color/normal_color"

      app:tabSelectedTextColor="@color/selected_color"

      <android.support.design.widget.TabLayout
                  android:id="@+id/tabs"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:tabGravity="fill"
                  app:tabTextAppearance="@style/CustomTabText"
                  app:tabTextColor="@color/text_color"
                  app:tabSelectedTextColor="@color/text_color"
                  app:tabMode="fixed" />
      

      对于自定义 TabLayout,在 getView() 方法中添加:

      tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector_textview));
      

      在你的drawable selector_textview.xml中添加这个:

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android" >
          <item android:state_selected="true" android:color="@color/selected_color" />
          <item android:state_focused="true" android:color="@color/selected_color" />
          <item android:state_pressed="true" android:color="@color/selected_color" />
          <item android:color="@color/normal_color" />
      
      </selector>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多