Change action bar tab text colour

别说谁变了你拦得住时间么 提交于 2020-01-06 12:43:46

问题


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


}

回答1:


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.



来源:https://stackoverflow.com/questions/21167693/change-action-bar-tab-text-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!