更改操作栏标签文本颜色 [英] Change action bar tab text colour

查看:168
本文介绍了更改操作栏标签文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用与刷卡操作栏中的应用程序。我有喜欢TAB1,TAB2,TAB3标题标签。我想设置不同颜色的每个选项卡标题的。 TAB1(红色)TAB2(蓝色)(TAB3)绿色。我已搜查和方式使用TabHost来了,但我不希望使用它。是否有任何其他的方式来实现这一目标。
下面是主要的活动

 公共类MainActivity扩展FragmentActivity工具
        ActionBar.TabListener {    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //设置操作栏。
        最后的动作条动作条= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        //创建将返回一个片段为三个的适配器
        //应用的主要部分。
        mSectionsPagerAdapter =新SectionsPagerAdapter(
                getSupportFragmentManager());        //设置的ViewPager与部分适配器。
        mViewPager =(ViewPager)findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);        //当不同部分之间滑动,选择对应的
        // 标签。我们也可以使用ActionBar.Tab#select()来做到这一点,如果我们有
        //为Tab的参考。
        mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
                    @覆盖
                    公共无效使用onPageSelected(INT位置){
                        actionBar.setSelectedNavigationItem(位置);
                    }
                });
        标签卡= actionBar.newTab().setIcon(R.drawable.prf).setText(getString(R.string.title_section1)).setTabListener(this);
        actionBar.addTab(标签,真正的);        标签= actionBar.newTab().setIcon(R.drawable.pack).setText(getString(R.string.title_section2)).setIcon(R.drawable.pack).setTabListener(this);
        actionBar.addTab(标签);        标签= actionBar.newTab().setIcon(R.drawable.call).setText(getString(R.string.title_section3)).setTabListener(this);
        actionBar.addTab(标签);        标签= actionBar.newTab().setIcon(R.drawable.prom).setText(getString(R.string.title_section4)).setTabListener(this);
        actionBar.addTab(标签);        标签= actionBar.newTab().setIcon(R.drawable.infoin).setText(getString(R.string.title_section5)).setTabListener(this);
        actionBar.addTab(标签);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    @覆盖
    公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
        //当选择给定的标签上,切换到相应的页面
        //的ViewPager。
        mViewPager.setCurrentItem(tab.getPosition());
    }    @覆盖
    公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
    }    @覆盖
    公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
    }    / **
     * A {@link FragmentPagerAdapter}返回对应片段
     *章节/标签/页面。
     * /
    公共类SectionsPagerAdapter扩展FragmentPagerAdapter {        公共SectionsPagerAdapter(FragmentManager FM){
            超(FM);
        }        @覆盖
        公共片段的getItem(INT位置){
            开关(位置){
            情况下0:
                返回首页新();
            情况1:
                返回新的信息();
            案例2:
                返回新计划();
            }
            返回null;
        }        @覆盖
        公众诠释的getCount(){
            //显示3总页​​数。
            返回3;
        }
    }
}


解决方案

您可以为每个选项卡自定义视图。创建标签一个新的布局资源(它可以只是一个TextView)。离开它的背景空,并为选择指标九宫绘制。使用获取LayoutInflater

  LayoutInflater吹气= getSystemService(LAYOUT_INFLATER_SERVICE);

然后为每个标签,你可以这样做:

 设置页标签= ab.newTab()
    .setText(TY1)
    .setTabListener(新MyTabListener(在此,TY1.class.getName()));
查看TabView的= inflater.inflate(R.layout.my_tab_layout,NULL);
tabView.setBackgroundColor(...); //设置自定义颜色
tab.setCustomView(TabView的);
ab.addTab(标签);

如果你想手动创建此,<一个href=\"http://stackoverflow.com/questions/18176867/use-different-color-for-different-actionbar-tab\">check这个帖子,你的问题将得到解决。

I have an application which uses action bar with swipes. I have tabs with title like Tab1, Tab2, Tab3. I want to set different color of each of tab title. Tab1 (Red) Tab2(Blue) (Tab3)green. I have searched and came up with way to use TabHost, But I don't want to use it. Is there any other way to achieve this. Here is main Activity

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;
        }
    }


}

解决方案

You can set a custom View for each tab. Create a new layout resource for the tab (it can just be a TextView). Leave its background empty and make a nine-patch drawable for the selection indicator. Get a LayoutInflater using

LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);

Then for each tab, you can do this:

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

If you want to create this manually, check This Post, and your problem will be solved.

这篇关于更改操作栏标签文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆