如何设置FragmentTabHost选项卡文本颜色

时间:2013-08-19 13:36:06

标签: android fragment-tab-host

如何设置FragmentTabHost标签文字颜色。我尝试了下面的代码,但它没有用。

((TextView) mTabHost.getCurrentTabView()
                .findViewById(android.R.id.title)).setTextColor(0xFFFFFFFF);


它让NPE说找不到TextView

2 个答案:

答案 0 :(得分:4)

这有点棘手。我使用了以下代码,它对我有用。

for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {

        final TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i)
                .findViewById(android.R.id.title);

            // Look for the title view to ensure this is an indicator and not a divider.(I didn't know, it would return divider too, so I was getting an NPE)
        if (tv == null)
            continue;
        else
            tv.setTextColor(0xFFFFFFFF);
}

答案 1 :(得分:1)

let's try this :
for example when you add your tab make your Indicator  : 

    TextView view = ....
    vew.setTextColor(...)

然后使用自定义视图设置setIndicator:

 mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(view),
                    FragmentStackSupport.CountingFragment.class, null);