gpt4 book ai didi

android - QTabWidget 改变底线颜色

转载 作者:行者123 更新时间:2023-11-29 01:45:10 27 4
gpt4 key购买 nike

我想改变 tabwidget 背景的颜色。 enter image description here我怎样才能改变蓝线的颜色??

我的代码是:

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

但它只会改变底部标签的背景颜色。

最佳答案

改为使用 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"/>

这也具有向后兼容性

关于android - QTabWidget 改变底线颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21918986/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com