要更改选项卡的文本颜色,您需要获取视图,即设置为选项卡标题的 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>
希望这会有所帮助。