更改操作栏选项卡的背景颜色

change background color of actionbar tabs

我知道这个话题已经被讨论过很多次了,但我遇到的问题是不同的。我使用样式更改了操作栏的颜色。但是当我使用操作栏创建选项卡时,选项卡的背景颜色没有改变。所以,问题是如何改变标签的颜色。其次,我想删除选项卡中的下划线。提前感谢任何解决方案。

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
Pager Adapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
 
 final int PAGE_COUNT = 2;
 
 /** Constructor of the class */
 public MyFragmentPagerAdapter(FragmentManager fm) {
  super(fm);
  
 }

 /** This method will be invoked when a page is requested to create */
 @Override
 public Fragment getItem(int arg0) {
  Bundle data = new Bundle();
  switch(arg0){
  
   /** Android tab is selected */
   case 0:
    CreateEvent createEvent = new CreateEvent();    
    data.putInt("current_page", arg0+1);
    createEvent.setArguments(data);
    return createEvent;
    
   /** Apple tab is selected */
   case 1:
    SearchEvent searchEvent = new SearchEvent();
    data.putInt("current_page", arg0+1);
    searchEvent.setArguments(data);
    return searchEvent; 
  }
  
  return null;
 }

 /** Returns the number of pages */
 @Override
 public int getCount() {  
  return PAGE_COUNT;
 }
 
}
Main FragmentActivity:

public class Event extends FragmentActivity{
 
 ActionBar mActionBar;
 ViewPager mPager;
 Activity myActivity;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.event);
  
  /** Getting a reference to action bar of this activity */
        mActionBar = getActionBar();
        
        
        
        /** Set tab navigation mode */
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        /** Getting a reference to ViewPager from the layout */
        mPager = (ViewPager) findViewById(R.id.pager);
        
        /** Getting a reference to FragmentManager */
        FragmentManager fm = getSupportFragmentManager();
        
        /** Defining a listener for pageChange */
        ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
         @Override
         public void onPageSelected(int position) {          
          super.onPageSelected(position);
          mActionBar.setSelectedNavigationItem(position);          
         }         
        };
        
        mPager.setOnPageChangeListener(pageChangeListener);
        
        MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
        
        mPager.setAdapter(fragmentPagerAdapter);

        mActionBar.setDisplayShowTitleEnabled(true);
        
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
   
   

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

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

   @Override
   public void onTabReselected(android.app.ActionBar.Tab tab,
     FragmentTransaction ft) {
    // TODO Auto-generated method stub
    
   }
  };

  /** Creating Android Tab */
        Tab tab = mActionBar.newTab()
                           .setIcon(R.drawable.ic_action_edit)
                           .setTabListener(tabListener);
        
        mActionBar.addTab(tab);

        /** Creating Apple Tab */
        tab = mActionBar.newTab()
                       .setIcon(R.drawable.ic_action_search)
                       .setTabListener(tabListener);                               

        mActionBar.addTab(tab);        

        
        
  
 }

尝试

mPager.setBackgroundColor(Color.BLACK);
mActionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));

更改 ActioBar 底线颜色。

<item name="android:background">@drawable/your_theme_name</item>
<item name="background">@drawable/your_theme_name</item>

并使用此代码更改选项卡的颜色

mActionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")