如何在TabLayout设置不同的文字和背景颜色?

时间:2019-01-30 15:35:14

标签: android android-studio android-tablayout

在我应用的ThemeActivity上,工具栏的颜色随主题而变化,可以是棕色,红色或粉红色。我想更改TabLayout上当前选定选项卡的文本和背景的颜色,以便它可以与工具栏的颜色匹配,并且也要更改其他选项卡的文本和背景的颜色(因此它们具有白色背景与深色文本)。

我遵循this answer,这很好,但是由于TabLayout是由他在style.xml上的样式定义的,所以我看不到每次更改标签后如何更改样式以及文字颜色。

我现在拥有的:

enter image description here(适用于所有颜色)

我想要什么:

enter image description here enter image description here

在ThemeActivity类:

public class ThemeActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_theme);

        initializeFields();
    }

    private void initializeFields() {
        String theme = getIntent().getStringExtra("Theme");

        mToolbar = findViewById(R.id.themeToolbar);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(theme);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        setToolbarColor(theme);

        mTabLayout = findViewById(R.id.themeTabLayout);
        mViewPager = findViewById(R.id.themeViewPager);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), ThemeActivity.this);
        mViewPager.setAdapter(pagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(pagerAdapter.getTabView(i));
        }
    }

    private void setToolbarColor(String theme) {
        if (theme.equals("Feminismo")) {
            mToolbar.setBackgroundColor(getResources().getColor(R.color.flatFeminism));
        } else if (theme.equals("Racismo")) {
            mToolbar.setBackgroundColor(getResources().getColor(R.color.flatRacism));
        } else {
            mToolbar.setBackgroundColor(getResources().getColor(R.color.flatLgbt));
        }
    }

    class PagerAdapter extends FragmentPagerAdapter {

        String tabTitles[] = new String[]{"Sobre", "Comunidades", "Artigos"};
        Context context;

        public PagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new AboutFragment();
                case 1:
                    return new CommunityFragment();
                case 2:
                    return new ArticlesFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return tabTitles.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles[position];
        }

        public View getTabView(int position) {
            View tab = LayoutInflater.from(ThemeActivity.this).inflate(R.layout.custom_tab, null);
            TextView tv = tab.findViewById(R.id.custom_text);
            tv.setText(tabTitles[position]);
            return tab;
        }

    }
}

TabLayout的style.xml中的部分

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
        <item name="tabBackground">@drawable/tab_background</item>
        <item name="tabIndicatorColor">#ff00ff</item>
        <item name="tabIndicatorHeight">2dp</item>
</style>

activity_theme.xml(标准视图)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ThemeActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/themeBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/themeToolbar"
            layout="@layout/main_toolbar"></include>

        <android.support.design.widget.TabLayout
            android:id="@+id/themeTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/themeToolbar">
    </android.support.design.widget.TabLayout>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/themeViewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/themeBarLayout"></android.support.v4.view.ViewPager>

</RelativeLayout>

的其余部分是作为丹尼尔纽金特回答相同的。 (tab_background.xml,tab_background_selected.xml,tab_background_unselected.xml,仅未选色我已经改变到#FFFFFF)

1 个答案:

答案 0 :(得分:0)

您只需要获取tabLayout并进行如下设置:

tablayout.setTabTextColors(Color.parseColor("#7DC1C6"), resources.getColor(R.color.white))

第一种颜色是正常颜色,第二种是所选颜色。

对于背景,您可以使用tabLayout.background