无法更改FragmentTabHost标签的指示器颜色

时间:2014-11-10 23:07:43

标签: android tabs styles fragment-tab-host

我想在我的应用中实现自定义FragmentTabHost。文档仅限于此主题,我无法使用自定义指示器drawables(绿色),它们不会显示在选项卡按钮下。

我只获得默认的蓝色指示器。有什么不对? 感谢。

...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        //actionBar.setDisplayShowTitleEnabled(false);        
        setContentView(R.layout.activity_main); 

        uiHelper = new UiLifecycleHelper(this, null);
        uiHelper.onCreate(savedInstanceState);      

        setContentView(R.layout.activity_main);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("Fidis", 
                getResources().getDrawable(R.drawable.tab_text_selector)),
                OffersFragmentActivity.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("Locatii", 
                getResources().getDrawable(R.drawable.tab_text_selector)),
                PlacesFragmentActivity.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab3").setIndicator("Harta", 
                getResources().getDrawable(R.drawable.tab_text_selector)),
                MapFragmentActivity.class, null);

        if(isFacebookLoggedIn()){
            System.out.println("We are logged in!");
        } else {
            showLogin();
        }

    }
...

tab_text_selector.xml

<!-- AOSP copyright notice can be found at the above link -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
</selector>

1 个答案:

答案 0 :(得分:0)

我有这样的问题,我用这个代码声明解决了它:

mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab_selected);
祝你好运!