tabLayout.setSelectedTabIndicatorColor(int color)不起作用

时间:2015-10-16 17:27:38

标签: android android-layout android-support-library androiddesignsupport android-tablayout

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我找到了解决此问题的方法。 在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>

完成了。我确实从主类中删除了以下代码

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

可能有更好的方法来解决这个问题,我不确定这是实现这一目标的确切方法。

答案 1 :(得分:0)

我也在尝试同样的方法,最后得到了解决方案。我尝试了所有,最后我尝试了下面的解决方案,它对我来说很有魅力。

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

            }
        });