尝试更改tabhost文本颜色,在此代码中我可以更改tabhost背景颜色(不是文本颜色)
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#FF0000")); // unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#0000FF")); // selected
}
});
如何更改tabhost文本颜色?
答案 0 :(得分:54)
您可以更改Tabhost文本的颜色,如下所示。
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.parseColor("#ffffff"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
tv.setTextColor(Color.parseColor("#000000"))
}
});
修改强>
要在活动中初始设置文字颜色,您可以在onResume()
功能
TabHost tabhost = getTabHost();
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#000000"));
}
答案 1 :(得分:9)
这实际上可以使用XML主题来完成。 TabWidget
对所选标签使用android:textColorPrimary
,对未选择标签使用android:textColorSecondary
。因此,您可以实现如下文本颜色更改:
在styles.xml中:
<style name="TabWidgetTheme" parent="AppTheme">
<item name="android:textColorPrimary">@color/your_primary_color</item>
<item name="android:textColorSecondary">@color/your_secondary_color</item>
</style>
在你的布局中:
<TabHost
android:layout_height="match_parent"
android:layout_width="match_parent"
android:theme="@style/TabWidgetTheme"/>
请注意,android:theme
不应直接在TabWidget
本身,而应包含TabHost
或类似内容。
答案 2 :(得分:4)
要更改选项卡的文本颜色,您需要获取视图,即TextView,它被设置为选项卡的标题,您可以像这样更改它:
TabHost tabhost = getTabHost();
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.parseColor("#000000"));
}
编辑:
另一种方法是为标签创建自定义视图。当您向tabHost
添加标签时FragmentTabHost tabHost;
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.frame);
//为每个标签创建customView 查看tabViewHome = createTabView(tabHost.getContext(),“Home”,R.drawable.ic_home);
tabHost.addTab(tabHost.newTabSpec("Home").setIndicator(tabViewHome), HomeActivity.class, null);
private static View createTabView(final Context context, final String text, int iconId)
{
// inflate your layout here
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
TextView tv = (TextView) view.findViewById(R.id.tab_tv_title);
tv.setText(text);
tv.setTextColor(Color.RED);
ImageView iv = (ImageView) view.findViewById(R.id.tab_background_iv_icon);
iv.setImageResource(iconId);
return view;
}
你的tab_layout.xml将是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:gravity="center"
android:orientation="vertical"
android:padding="5dip"
android:background="#AAE1E1E1">
<ImageView
android:id="@+id/tab_background_iv_icon"
android:layout_width="30dip"
android:layout_height="30dip"
android:contentDescription="@string/imgDesc"
/>
<TextView
android:id="@+id/tab_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:textColor="@drawable/tab_text_selector"
android:textSize="8sp"
android:textStyle="bold" />
</LinearLayout>
希望这有帮助。
答案 3 :(得分:0)
Ehi man,我用这个解决方案:
private void setNewTab(final String tag, final String title, final Class<?> clazz, final Bundle bundle) {
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
tabSpec.setIndicator(InfoTabView_.build(getActivity()).bind(title, false));
tabHost.addTab(tabSpec, clazz, bundle);
}
...
bundle = new Bundle();
bundle.putSerializable(FaqFragment.ARG_FAQS, infos.getFaq());
setNewTab("faq", "Faq", FaqFragment_.class, bundle);
...
@EViewGroup(R.layout.view_info_tab)
public class InfoTabView extends RelativeLayout {
....
@Override
public void setSelected(final boolean selected) {
if (selected)
titleTextView.setTextColor(selectedColor);
else
titleTextView.setTextColor(unselectedColor);
}
}
覆盖setSelected是最干净的方式!