android – 在代码中设置所选项目时,Tablayout的文本颜色不会更改

我试图让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.我发现了一个错误吗?

最佳答案 如果您在选择tabLayout xml时想要TabLayout颜色更改应该是这样的:

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

            }
        });
点赞