是否可以以编程方式更改操作栏选项卡指示器 [英] Is it possible to change actionbar tab indicator programmatically

查看:17
本文介绍了是否可以以编程方式更改操作栏选项卡指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i change programmatically the selected tab indicator of my action bar ? i have read about tab styling, and Tab.setCustomView() method, but none of these helps :

  • 使用选项卡样式,我可以更改指示器颜色,但它会保留在所有选项卡中(我希望每个选项卡都有一个指示器).

  • With tab styles, i can change the indicator color, but it will remain for all tabs (i want to have an indicator for each tab).

对于标签自定义视图,我使用了一个布局,其中 TextView 用于标签标题,View 用于管理指示器颜色.在 java 中,我动态更改 View 的背景,但问题是 View 的背景与选项卡边界不匹配.

With tab custom view, i have used a layout with a TextView for tab title, and View for managing the indicator color. In the java i change the View's background dynamically, but the problem with that is the View's background doesn't match tabs bounds.

<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"/>

谁能告诉我哪里错了?还有另一种方法吗?谢谢

Can somebody tell me where i'am wrong ? Is there another way to do it ? Thanks

推荐答案

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

I have succeeded to implement what i wanted by using @Padma's answer to generate my tab indicator backgrounds : i needed 5 selectors : green, yellow, blue, orange and red. So i created 5 xml drawables (tabs_selector_red.xml, tabs_selector_blue.xml, etc...) :

tabs_selector_green.xml:

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

I also created a layer-list for each xml background : 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 动态切换背景购买:

And finally, in the Java, i switch the background dynamically buy using selected tab's custom view and index :

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);
/* ... */
}

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

Now let's add some screenshots :

这篇关于是否可以以编程方式更改操作栏选项卡指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆