Colors 更改TabLayout的选定选项卡背景和文本颜色

Colors 更改TabLayout的选定选项卡背景和文本颜色,colors,title,android-tablayout,Colors,Title,Android Tablayout,我想从Java更改所选选项卡的背景和文本颜色。 我从Android studio内置活动创建了这个活动,并为我做了一些更改。 我的代码是 public class BanglalinkInternetPack extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section content

我想从Java更改所选选项卡的背景和文本颜色。 我从Android studio内置活动创建了这个活动,并为我做了一些更改。 我的代码是

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);
使用此方法更改选项卡标题的颜色,保持参数不变,以防您不想更改所选选项卡的颜色,并选中此项

使用
app:tabSelectedTextColor
app: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"
    />