2014-02-20 98 views
2

我想更改tabwidget背景的顏色。 enter image description here我怎樣才能改變藍線的顏色?TabWidget changr底線顏色

我的代碼是:

public class MainActivityNew extends ActionBarActivity implements 
    ActionBar.TabListener { 
private FragmentTabHost mTabHost; 
private int oldButtonId = 1; 
private ViewPager viewPager; 
private TabsPagerAdapter mAdapter; 
private ActionBar actionBar; 
private Database db; 
private FrameLayout frame; 
// Tab titles 
private String[] tabs = {"Новости", "Мои купоны"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main_activity); 

} 

private void initialization() { 
    frame = (FrameLayout) findViewById(R.id.content_frame); 
    viewPager = (ViewPager) findViewById(R.id.pager); 
    actionBar = getSupportActionBar(); 
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), actionBar, frame, viewPager); 
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(this, getSupportFragmentManager(), R.id.content_frame); 

    ///////////////Add now 16/01/2014 
    Bundle b = new Bundle(); 
    mTabHost.getTabWidget().setDividerDrawable(R.color.tabTransparent); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageDrawable(getResources().getDrawable(R.drawable.news_icon_selector)); 
    mTabHost.addTab(mTabHost.newTabSpec("1") 
      .setIndicator(tabIndicator), 
      FragmentNews.class, b); 


    tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false); 
    icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageDrawable(getResources().getDrawable(R.drawable.catalog_icon_selector)); 
    mTabHost.addTab(mTabHost.newTabSpec("2") 
      .setIndicator(tabIndicator), FragmentCatalog.class, b); 


    tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_center,mTabHost.getTabWidget(), false); 
    icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageDrawable(getResources().getDrawable(R.drawable.my_cards_icon_selector)); 
    mTabHost.addTab(mTabHost.newTabSpec("3"). 
      setIndicator(tabIndicator),FragmentMyCards.class, b); 

    tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false); 
    icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageDrawable(getResources().getDrawable(R.drawable.near_icon_selector)); 
    mTabHost.addTab(mTabHost.newTabSpec("4"). 
      setIndicator(tabIndicator),FragmentNear.class, b); 

    tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_new,mTabHost.getTabWidget(), false); 
    icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageDrawable(getResources().getDrawable(R.drawable.settings_icon_selector)); 
    mTabHost.addTab(mTabHost.newTabSpec("5"). 
      setIndicator(tabIndicator), FragmentSettingsPartOne.class, b); 

    mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String tabId) { 
      setActivityView(Integer.valueOf(tabId)); 
      Log.d("tabs", "tabID=" + tabId); 
     } 
    }); 
    ////////////////////end of adding 
    viewPager.setAdapter(mAdapter); 
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

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

    } 
    setButtonsColor(1, 3); 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    viewPager.setCurrentItem(tab.getPosition()); 
} 

public void setActivityView(int newButtonId) { 

    switch (newButtonId) { 
     case 1: 
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
      actionBar.show(); 
      viewPager.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 0, 8.0f)); 
      frame.setLayoutParams(new LinearLayout.LayoutParams(0, 0, 0f)); 
      break; 
     default: 
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
      actionBar.hide(); 
      frame.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 0, 8.0f)); 
      viewPager.setLayoutParams(new LinearLayout.LayoutParams(0, 0, 0f)); 

      break; 
    } 




} 

@Override 
protected void onResume() { 
    initialization(); 
    db = Database.getInstance(this); 


    /** 
    * on swiping the viewpager make respective tab selected 
    * */ 

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageSelected(int position) { 
      // on changing the page 
      // make respected tab selected 
      actionBar.setSelectedNavigationItem(position); 
     } 

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

     @Override 
     public void onPageScrollStateChanged(int arg0) { 
     } 
    }); 
    super.onResume(); /
} 
} 

我已閱讀this我嘗試下

TabWidget widget = mTabHost.getTabWidget(); 
    for(int i = 0; i < widget.getChildCount(); i++) { 
     View v = widget.getChildAt(i); 

     // Look for the title view to ensure this is an indicator and not a divider. 
     TextView tv = (TextView)v.findViewById(android.R.id.title); 
     if(tv == null) { 
      continue; 
     } 
     // v.setBackgroundResource(R.drawable.your_tab_selector_drawable); 
     v.setBackgroundColor(Color.RED); 
    } 

但它只是改變底部標籤的背景顏色。

+0

你能做到這一點?我面臨同樣的問題 – Virthuss

回答

1

使用android.support.design.widget.TabLayout代替

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_layout" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/actionbar" 
    app:tabIndicatorColor="@color/tab_indicator_color" // this is what you want 
    app:tabIndicatorHeight="3dp" // tab indicator height 
    app:tabSelectedTextColor="@color/white" 
    app:tabTextColor="@color/font_color_white70"/> 

這具有向後兼容性也