如何更改选项卡片段(Viewpager)中tabBar的颜色

时间:2015-06-01 06:45:51

标签: android android-layout android-fragments android-viewpager

我尝试了很多次但是不行。 我尝试在MainActivity中更改actionBar,但TabBar没有更改,所以我不知道如何更改它。

这是我的代码: MainActivivty

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private ActionBar actionBar;
private TabsPagerAdapter mAdapter;

private String[] tabs = { "Bài Hát", "Yêu Thích"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar ac = getActionBar();
    ac.setBackgroundDrawable(new ColorDrawable(Color.rgb(72, 209, 204)));
    //ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E64260")));


    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();

    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mAdapter);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    viewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            if (position == 1) {
                TabYeuThich frag = (TabYeuThich) mAdapter
                        .getFragmentTabYeuThich(1);
                frag.resetPage();
            } else {
                TabBaiHat frag = (TabBaiHat) mAdapter
                        .getFragmentTabBaiHat(0);
                frag.resetPage();
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    viewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

这是类:TabsPagerAdapter

public class TabsPagerAdapter extends FragmentPagerAdapter {

FragmentManager fm;
TabYeuThich fragYeuThich;
TabBaiHat fragBaiHat;

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
    this.fm = fm;
}

@Override
public Fragment getItem(int index) {
    // TODO Auto-generated method stub
    switch (index) {
    case 0:
        fragBaiHat = new TabBaiHat();
        return fragBaiHat;
    case 1:
        fragYeuThich = new TabYeuThich();
        return fragYeuThich;
    }

    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 2;
}

public Fragment getFragmentTabYeuThich(int post) {

    return fragYeuThich;

}

public Fragment getFragmentTabBaiHat(int post) {

    return fragBaiHat;

}

请帮帮我!谢谢大家!

1 个答案:

答案 0 :(得分:0)

有用的我从themes.xml设置了操作栏 我建议你跟进这两个教程

developer android

action bar explanation