是否可以通过编程方式更改操作栏选项卡指示器

时间:2013-08-28 11:26:47

标签: android tabs actionbarsherlock android-actionbar

如何以编程方式更改操作栏的选定选项卡指示器?我已阅读有关tab styling和Tab.setCustomView()方法的信息,但这些都没有帮助:

  • 使用标签样式,我可以更改指示器颜色,但它将保留所有标签(我希望每个标签都有一个指示器)。

  • 使用标签自定义视图,我使用了一个布局,标签标题为TextView,而View用于管理指标颜色。在java中,我动态更改了View的背景,但问题是View的背景与标签边界不匹配。

<TextView
    android:id="@+id/custom_tab_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:gravity="center|center_horizontal"
    android:textStyle="bold"/>

<View 
    android:id="@+id/custom_tab_view"
    android:layout_width="match_parent"
    android:layout_height="10dp" 
    android:layout_alignParentBottom="true"/>

有人可以告诉我哪里错了吗?还有另一种方法吗?感谢

2 个答案:

答案 0 :(得分:17)

通过使用@ Padma的答案生成我的标签指示器背景,我已经成功实现了我想要的东西:我需要5个选择器:绿色,黄色,蓝色,橙色和红色。所以我创建了5 xml drawablestabs_selector_red.xml, tabs_selector_blue.xml, etc...):

tabs_selector_green.xml:

    <!-- Non focused states -->
<item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

<!-- Focused states -->
<item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

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

<!-- Focused states -->
<item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@drawable/layer_bg_selected_tabs_green" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>

我还为每个xml背景创建了一个layer-list: layer_bg_selected_tabs_green.xml

<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/tab_green" />

        <padding android:bottom="5dp" />
    </shape>
</item>
<item>
    <shape android:shape="rectangle" >
        <solid android:color="#FFFFFF" />
    </shape>
</item>

最后,在Java中,我使用所选标​​签custom viewindex动态切换背景:

private static final int[] TABS_BACKGROUND = {
        R.drawable.tabs_selector_orange, R.drawable.tabs_selector_green,
        R.drawable.tabs_selector_red, R.drawable.tabs_selector_blue,
        R.drawable.tabs_selector_yellow };
/*
BLA BLA BLA
*/
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView();
    tabLayout.setBackgroundResource(TABS_BACKGROUND[tab.getPosition()]);
    tab.setCustomView(tabLayout);
/* ... */
}

现在让我们添加一些截图:

green blue red

答案 1 :(得分:5)

//你的标签栏应该是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Wrap"
    android:background="@drawable/tabs_selector"
    android:gravity="center_horizontal|bottom"
    android:minHeight="@dimen/size_fourty"
     >
    <TextView
    android:id="@+id/custom_tab_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    android:gravity="center|center_horizontal"
    android:textStyle="bold"/>

</RelativeLayout>

//您的tabs_selector.xml应该喜欢这个

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

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

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

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

//你的layer_bg_unselected_tabs应该是这样的

<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/red" />

        <padding android:bottom="2dp" />
    </shape>
</item>
<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/gray" />
    </shape>
</item>

//你的layer_bg_selected_tabs应该是这样的

<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/red" />

        <padding android:bottom="8dp" />
    </shape>
</item>
<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/gray" />
    </shape>
</item>