自定义TabHost / TabSpec不起作用

时间:2015-06-01 09:36:22

标签: android android-tabhost

我在initialiseTabHost()中遇到了一些问题。当我在setIndicator中给出一个drawable时,它不起作用。

TabActivity已弃用,因此我使用了FragmentActivity。大多数教程都没有说明自定义指标。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Step 1: Inflate layout
    setContentView(R.layout.evenement);
    // Step 2: Setup TabHost
    initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
    }
}

protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
    super.onSaveInstanceState(outState);
}

private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    FragmentListEvents.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("After").setIndicator("",
            getResources().getDrawable(R.drawable.tab_bg_selector)), (tabInfo = new TabInfo("After", Event_After.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    FragmentListEvents.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Before").setIndicator("Before"), (tabInfo = new TabInfo("Before", Event_Before.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);

    // Default to first tab
    this.onTabChanged("Tab1");
    //
    mTabHost.setOnTabChangedListener(this);
}

tab_bg_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@android:color/transparent" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@android:color/transparent" />

</selector>

tab_bg_selected

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#607F2F"
        android:endColor="#99C357" android:angle="-90" />
</shape>

谢谢你的答案。

0 个答案:

没有答案