我想改变tabwidget背景的颜色。 如何更改蓝线的颜色?
我的代码是:
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);
}
但它只会改变底部标签的背景颜色。