2014-01-16 114 views
0

我有一個使用操作欄與滑動的應用程序。我有Tab1,Tab2,Tab3等標題的標籤。我想爲每個標題標題設置不同的顏色。 Tab1(紅色)Tab2(藍色)(Tab3)綠色。我已經搜索並提出了使用TabHost的方法,但我不想使用它。有沒有其他方法可以實現這一點。 這裏是主要的活動更改操作欄選項卡文本顏色

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


} 

回答

0

您可以爲每個選項卡自定義視圖。爲選項卡創建新的佈局資源(它可以只是一個TextView)。保持其背景爲空,併爲選擇指示器繪製九個補丁。獲取使用

LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE); 

然後爲每個標籤一個LayoutInflater,你可以這樣做:

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

如果你想手動創建此,check This Post,你的問題將得到解決。