Android 更改操作栏选项卡文本颜色

Android 更改操作栏选项卡文本颜色,android,xml,android-fragments,android-viewpager,Android,Xml,Android Fragments,Android Viewpager,我有一个应用程序,它使用带有滑动的动作栏。我有一些标签,标题像Tab1,Tab2,Tab3。我想设置每个选项卡标题的不同颜色。表1红色表2蓝色表3绿色。我已经搜索并想出了使用TabHost的方法,但我不想使用它。有没有其他方法可以做到这一点。 这里是主要活动 public class MainActivity extends FragmentActivity implements ActionBar.TabListener { SectionsPagerAdapter

我有一个应用程序,它使用带有滑动的动作栏。我有一些标签,标题像Tab1,Tab2,Tab3。我想设置每个选项卡标题的不同颜色。表1红色表2蓝色表3绿色。我已经搜索并想出了使用TabHost的方法,但我不想使用它。有没有其他方法可以做到这一点。 这里是主要活动

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

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

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

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

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });


        Tab tab = actionBar.newTab().setIcon(R.drawable.prf).setText(getString(R.string.title_section1)).setTabListener(this);
        actionBar.addTab(tab, true);

        tab = actionBar.newTab().setIcon(R.drawable.pack).setText(getString(R.string.title_section2)).setIcon(R.drawable.pack).setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setIcon(R.drawable.call).setText(getString(R.string.title_section3)).setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setIcon(R.drawable.prom).setText(getString(R.string.title_section4)).setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setIcon(R.drawable.infoin).setText(getString(R.string.title_section5)).setTabListener(this);
        actionBar.addTab(tab);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
    }

    /**
     * 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) {
            switch (position) {
            case 0 :
                return new Home();
            case 1:
                return new info();
            case 2:
                return new Plan();
            }
            return null;
        }

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


}

可以为每个选项卡设置自定义视图。为选项卡创建一个新的布局资源,它可以只是一个文本视图。将其背景留空,并为选择指示器绘制一个9面片。使用

LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);
然后,对于每个选项卡,可以执行以下操作:

Tab tab = ab.newTab()
    .setText("TY1")
    .setTabListener(new MyTabListener(this, TY1.class.getName()));
View tabView = inflater.inflate(R.layout.my_tab_layout, null);
tabView.setBackgroundColor(...); // set custom color
tab.setCustomView(tabView);
ab.addTab(tab);
如果您想手动创建,您的问题将得到解决