如何在应用程序工具栏中增加菜单项图标和标题之间的填充或边距? [英] How to increase padding or margin between menu item icon and title in app toolbar?

查看:55
本文介绍了如何在应用程序工具栏中增加菜单项图标和标题之间的填充或边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的操作栏中的某个项目有问题:

I'm having a problem with an item in my action bar:

刷新图标太靠近标题"REFRESH".看起来很尴尬.

The refresh icon is too close to the title "REFRESH". It looks awkward.

我知道,有一种方法可以解决此问题,即编辑图像以强制向其中添加填充,或将其包装在XML可绘制的容器中.

I am aware that one approach to fix this it to edit the image to forcibly add padding to it, or wrap it in an XML drawable that does so.

似乎我不必在图像本身中添加空间来解决此问题.我觉得应该有一种简单的方法来更改padding或margin属性来解决此问题.我很惊讶他们在这里这么近.

It seems like I should not have to add space in the image itself to fix this. I feel that there should be an easy way to change a padding or margin attribute to fix the problem. I'm surprised they are so close together here.

有什么想法吗?

推荐答案

我不敢相信我花了这么长时间才弄清楚这一点!

I can't believe it took me so long to figure this out!

最终我想到要使用Android设备监视器.我去了工具>安卓>安卓设备监视器.启动时,我在设备"选项卡中单击了我的应用程序的程序包名称,然后单击了顶部的"UI Automator的转储视图层次结构"按钮(看起来像是多个电话堆叠在一起). 设备"标签.这向我显示了当前屏幕的用户界面.然后,我可以单击刷新"按钮,发现它不是彼此相邻的两个视图,而是一个带有DrawableLeft的TextView.我知道,我知道,这很明显,应该是我想到的第一件事.

Eventually it occurred to me to use the Android Device Monitor. I went to Tools > Android > Android Device Monitor. When that started up, I clicked the package name of my app in the "Devices" tab and then I clicked the "Dump View Hierarchy for UI Automator" button (which looks like multiple phones stacked on top of each other) at the top of the "Devices" tab. This showed me the UI of my current screen. Then I could click on the "Refresh" button and discover that it was not two Views next to each other--it was one TextView with a DrawableLeft. I know, I know, that's obvious and should have been the first thing I thought of.

我已经找到了 ActionBarSherlock-如何设置每个操作栏图标的边框?,但这对我不起作用.但是,这为我提供了一个我需要的示例-更改了ActionButton样式.

I had already found ActionBarSherlock - How to set the padding of each actionbar's icon? , but that didn't work for me. However, that gave me an example of something I needed--changing the ActionButton style.

现在我知道它是一个带有DrawableLeft的TextView,我知道我需要更改DrawablePadding.这就是我需要对样式进行的更改.

Now that I knew it was a TextView with a DrawableLeft, I knew I needed to change DrawablePadding. That's the change I needed to make to styles.

要更改ActionButton主题,我需要将此主题添加到我的styles.xml中:

To change the ActionButton theme I needed to add this to my styles.xml:

<style name="ActionButton" parent="@android:style/Widget.ActionButton">
    <item name="android:drawablePadding">@dimen/action_button_padding</item>
</style>

dimen.xml:

dimen.xml:

<dimen name="action_button_padding">4dp</dimen>

然后对style.xml的主题部分进行调整:

Then a tweak to the Theme section of style.xml:

<style name="Theme.MyTheme"
       parent="Theme.AppCompat.Light.NoActionBar">
    <!-- other styles -->
    <item name="android:actionButtonStyle">@style/ActionButton</item>
</style>

它就可以了!无需设置自定义ActionProvider,编辑图像以在图像文件中添加间距,也无需为每个按钮使用额外的XML文件.

And it just works! No need to set a custom ActionProvider, edit the image to add spacing in the image file, or use extra XML files for each button.

这篇关于如何在应用程序工具栏中增加菜单项图标和标题之间的填充或边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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