改变TabLayout的选中标签背景和文字颜色

Change the selected tab background and text color of TabLayout

我想更改从 Java 中选择的选项卡背景和文本颜色。 我从 Android studio build in activity 创建了这个 activity 并为我做了一点改变。 我的密码是

public class BanglalinkInternetPack extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_banglalink_internet_pack);
    //getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f37022")));

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}


/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        switch (position){
            case 0:
                return new Montly_pack_bl();

            case 1:
                return  new Daily_packs_bl();

            case 2:
                return  new Weekly_pack_bl();

            case 3:
                return  new Montly_pack_bl();

        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 4 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Most Used";
            case 1:
                return "Daily";
            case 2:
                return "Weekly";
            case 3:
                return "Monthly";
        }
        return null;
    }

}

}

我需要更改哪里?

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
// void setTabTextColors(int normalcolour, int selectedcolour)
    tabLayout.setTabTextColors(-1,-256);

使用此方法更改选项卡标题的颜色,保持参数不变以防您不想更改所选上的颜色并选中此 https://developer.android.com/reference/android/support/design/widget/TabLayout.html#setSelectedTabIndicatorColor(int)

使用app:tabSelectedTextColorapp:tabTextColor:

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@color/blue"
    app:tabSelectedTextColor="@color/blue"
    app:tabTextColor="@color/black"
    />