如何更改操作栏选项卡背景颜色?

时间:2014-08-20 05:56:11

标签: android android-actionbar

我需要更改背景标签颜色我在这个上花了很多时间,但是我无法更改背景颜色请帮助我如何更改标签背景颜色。

public class MapTabPagerActivity extends BaseActivity {

    private ViewPager mViewPager;

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

        mViewPager = (ViewPager) findViewById(R.id.pager);

        MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());

        mViewPager.setAdapter(pagerAdapter);

        ActionBar actionBar = getSupportActionBar();


        actionBar.addTab(actionBar.newTab().setText("Locations").setTabListener(tabListener));

        actionBar.addTab(actionBar.newTab().setText("Map").setTabListener(tabListener));

        // Specify that tabs should be displayed in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        // When swiping between pages, select the
                        // corresponding tab.
                        getSupportActionBar().setSelectedNavigationItem(position);
                    }
                });

    }

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

            mViewPager.setCurrentItem(tab.getPosition());

        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

    };


    private class MyPagerAdapter extends FragmentPagerAdapter{

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

        @Override
        public Fragment getItem(int pos) {

            switch (pos) {
            case 0:
                AtmLocationFragment listFragment = new AtmLocationFragment();
                return listFragment;
            case 1:
                AtmMapFragment mapFragment = new AtmMapFragment();
                return mapFragment;
            default:
                break;
            }

            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }

    }

}

enter image description here

2 个答案:

答案 0 :(得分:0)

这很简单:)

actionBar.setStackedBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_brown_dark)));
在您的情况下,

或更简单:

actionBar.setBackgroundDrawable(new ColorDrawable("FF0000"));

答案 1 :(得分:0)

以您的风格添加:

   <style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
   </style>

   <style name="MyActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
     <item name="android:backgroundStacked">@drawable/my_stacked_background</item>
   </style>**
相关问题