如何在操作栏中更改选项卡指示符下划线高度

时间:2015-08-08 10:49:30

标签: android-actionbar android-actionbar-compat

在我的操作栏中,我想最小化所选标签下划线的高度。我试图在drawable中最小化tab_selected.9.png,tab_selected_focused.9.png和tab_selected_pressed.9.png的高度。但没有任何影响,我想我是按照错误的方式。对于actionbar和tab,我使用android.support.v7.app.ActionBar和android.support.v7.app.ActionBar.Tab。任何建议都非常感谢。

感谢

enter image description here

theme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@style/Theme.AppCompat.Light.DarkActionBar">

    <!-- Support library compatibility -->
    <item name="actionBarTabStyle">@style/MyActionBarTabs</item>

</style>

<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
       parent="@style/Widget.AppCompat.ActionBar.TabView">
    <!-- tab indicator -->
    <item name="android:background">@drawable/actionbar_tab_indicator</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/actionbar_tab_indicator</item>
</style>
</resources>

actionbar_tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

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

<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:state_focused="true" android:state_selected="false"
      android:state_pressed="false"
      android:drawable="@drawable/tab_unselected_focused" 
      />
<item android:state_focused="true" android:state_selected="true"
      android:state_pressed="false"
      android:drawable="@drawable/tab_selected_focused" />


<!-- STATES WHEN BUTTON IS PRESSED -->

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

<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:state_focused="true" android:state_selected="false"
      android:state_pressed="true"
      android:drawable="@drawable/tab_unselected_pressed" />
<item android:state_focused="true" android:state_selected="true"
      android:state_pressed="true"
      android:drawable="@drawable/tab_selected_pressed" />
</selector>

1 个答案:

答案 0 :(得分:1)

我在此链接中找到了答案 - Is it possible to change actionbar tab indicator programmatically

使用9个补丁图像作为选项卡指示符的实例,我在tab_selected.xml,tab_selected_focused.xml,tab_selected_pressed.xml,tab_unselected.xml,tab_unselected_focused.xml和tab_unselected_pressed.xml中使用了layout-list。

tab_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape android:shape="rectangle" >
    <solid android:color="#ffffff" />

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

tab_selected_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/actionbar_color" />
        </shape>
  </item>
</layer-list>

tab_selected_focused.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/actionbar_color" />
        </shape>
  </item>
</layer-list>

tab_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


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

</layer-list>

tab_selected_unfocused.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/actionbar_color" />
        </shape>
  </item>
</layer-list>

tab_selected_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/actionbar_color" />
        </shape>
  </item>
</layer-list>

由于