是否有可能以编程方式更改动作条选项卡指示器指示器、有可能、选项卡、动作

2023-09-12 00:50:27 作者:我是天使也是贱货

我怎样才能改变我的编程操作栏上的选择选项卡指示器?我看了一下标签造型和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 为分页标题,而查看管理指示灯​​的颜色。在Java I'动态的背景,但这个问题是查看修改查看的背景没有按T匹配的标签范围。

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

推荐答案

我已经成功地实现了我想用@帕德玛的回答来生成我的标签指示的背景:我需要5选择:绿,黄,蓝,橙红。所以我创建5 XML可绘制 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_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 ,我切换后台动态地购买使用选定的选项卡的自定义视图首页

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 :