Is there a way to reduce the spacing between the Action Item Icons on Action Bar?

不羁岁月 提交于 2019-11-27 03:20:28

use a custom android:actionButtonStyle.

<item name="android:actionButtonStyle">@style/ActionButtonStyle</item>

and the magic is here:

<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:minWidth">0dip</item>
    <item name="android:paddingLeft">0dip</item>
    <item name="android:paddingRight">0dip</item>                  
</style>

normaly minWidth is set to 56 dip.

For SDK versions 17+ you should use paddingStart and paddingEnd properties, if you have minSdkVersion < 17 you must use different styles for each version, like this:

res/values/styles.xml :

<item name="android:actionButtonStyle">@style/ActionButtonStyle</item>

<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:minWidth">0dip</item>
    <item name="android:paddingLeft">0dip</item>
    <item name="android:paddingRight">0dip</item>                  
</style>

res/values-v17/styles.xml :

<item name="android:actionButtonStyle">@style/ActionButtonStyle</item>

<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:minWidth">0dip</item>
    <item name="android:paddingStart">0dip</item>
    <item name="android:paddingEnd">0dip</item>                  
</style>

Thanks to Shai for his hint, that works pretty well for me (on an Android 4.0.x device): (my whole style.xml)

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionButtonStyle">@style/ActionButtonStyle</item>
</style>

<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:minWidth">0dip</item>
    <item name="android:paddingLeft">0dip</item>
    <item name="android:paddingRight">0dip</item>
</style>

For supporting older version using appCompat you need nothing but this:

In your application's appTheme :

<item name="actionButtonStyle">@style/ActionButtonStyle</item>

and create a style in style.xml

 <!--this will reduce space between actionBar Icons-->
    <style name="ActionButtonStyle" parent="Widget.AppCompat.ActionButton">
        <item name="android:minWidth">0dip</item>
        <item name="android:maxWidth">50dip</item>
        <item name="android:paddingLeft">0dip</item>
        <item name="android:paddingRight">0dip</item>
    </style>

That's it. You are done.

In appTheme add

<item name="actionButtonStyle">@style/CustomActionButtonStyle</item>

and then in style.xml create this style

<style name="CustomActionButtonStyle" parent="Widget.AppCompat.ActionButton">
        <item name="android:paddingStart">0dip</item>
        <item name="android:paddingEnd">0dip</item>
    </style>
hankystyles

I think you may find that this is hard-to-impossible to accomplish without getting extremely hackish or just creating your own custom ActionBar. See here for what seems like a pretty similar question. I did some testing myself overriding android:actionButtonStyle, and while I was able to override certain things (like the background), it refused to honor any kind of padding attributes i specified.

None of the answers worked for me. So I wrapped the icon in a drawable xml using layer-list and give it a negative left margin, and it worked for me this way.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:left="-20dp">
        <bitmap
            android:src="@drawable/actionbar_logo"
            android:gravity="left" />
    </item>
</layer-list>

If you are creating a custom ActionBar, you can add custom spacing, between elements, if you want to move items closer to each other, you can use negative values for your attributes. Like this pseudo-code (in your layout xml file):

<ImageView 
android:layout_marginLeft="-2dp"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!