tabLayout.setSelectedTabIndicatorColor(整数颜色)不工作

tabLayout.setSelectedTabIndicatorColor(int color) not working

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("All Calculators"));
        tabLayout.addTab(tabLayout.newTab().setText("My Calculators"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

//        tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#71CDF5"));

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
//does not change the tab's title color when selected
        tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#71CDF5"));
    }

我做错了什么?

我确实找到了解决此问题的方法。 在 styles.xml 中,我添加了以下代码。

<style name="AppTheme.TabStyle" parent="Widget.Design.TabLayout">
        <item name="tabTextAppearance">@style/AppTheme.TextStyle</item>
        <item name="tabTextColor">#000000</item>
        <item name="tabSelectedTextColor">#71CDF5</item>
        <!--<item name="tabIndicatorColor">@color/primaryDark</item>-->
        <!--<item name="tabIndicatorHeight">3dp</item>-->
        <!--<item name="android:background">?attr/colorPrimary</item>-->
    </style>

    <style name="AppTheme.TextStyle" parent="TextAppearance.Design.Tab">
        <item name="android:textSize">14sp</item>
        <item name="textAllCaps">false</item>
    </style>

然后在我的选项卡布局中添加了样式

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#3B4245"
    style="@style/AppTheme.TabStyle"
    android:elevation="6dp">

</android.support.design.widget.TabLayout>

完成了。我确实从主 class

中删除了以下代码
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#71CDF5"));

也许有更好的方法来解决这个问题,我不确定这是实现此目的的确切方法。

我也在尝试同样的方法,终于找到了解决方案。我尝试了所有方法,最后尝试了以下解决方案,它对我很有用。

viewPager.setOnPageChangeListener(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) {

            }
        });