如何在android中的菜单项之间添加填充?

如何在android中的菜单项之间添加填充?,android,android-layout,android-actionbar,Android,Android Layout,Android Actionbar,我将菜单项从res/menu/menu.xml扩展到ActionBar,如何使用android在菜单项之间添加填充 <item android:id="@+id/home" android:showAsAction="always" android:title="Home" android:icon="@drawable/homeb"/> <item android:id="@+id/location" android:sho

我将菜单项从res/menu/menu.xml扩展到ActionBar,如何使用android在菜单项之间添加填充

<item 
    android:id="@+id/home"
    android:showAsAction="always"
    android:title="Home"
    android:icon="@drawable/homeb"/>
<item 
    android:id="@+id/location"
    android:showAsAction="always"
    android:title="Locations"
    android:icon="@drawable/locationb"/>
<item
    android:id="@+id/preQualify"
    android:showAsAction="always"
    android:title="Pre-Qualify"
    android:icon="@drawable/prequalityb"/>
<item 
    android:id="@+id/products"
    android:showAsAction="always"
    android:title="Products"
    android:icon="@drawable/productb"/>


填充我不知道,但您可以通过以下方式添加边距:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
yourItem.setLayoutParams(lp);
设置值,而不是“左、上、右、下”。在本例中,我在一个项目上添加了边距,您可以通过其ID获得边距


希望这有帮助。

创建可绘制的xml。比如
res/drawable/shape\u green\u rect.xml

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

  <!-- Specify a semi-transparent solid green background color -->
  <solid android:color="#5500FF66" />

  <!-- Specify a dark green border -->
  <stroke 
    android:width="5dp"
    android:color="#009933" />

  <!-- Specify the margins that all content inside the drawable must adhere to -->
  <padding
    android:left="30dp"
    android:right="30dp"
    android:top="30dp"
    android:bottom="30dp" />
</shape>
这样我们就可以在菜单中添加填充


谢谢。

找到了解决方案,在styles.xml文件中添加了以下几行,它成功了

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>

<style name="MyActionButtonStyle" parent="AppBaseTheme">
    <item name="android:minWidth">20dip</item>
    <item name="android:padding">0dip</item>
</style>

@样式/MyActionButtonStyle
20度
0度

谢谢您的回复!!如果我添加
android:icon=“@drawable/shape\u green\u rect”
在xml文件中我在哪里添加图标?重要的是“minWidth”,默认情况下是80dp。这意味着即使没有填充,也会有一些空间。这就是我的问题所在。如何在我的菜单项中使用此样式??如果您共享简单的代码,这将很有帮助。我注意到我们的主题样式和“MyActionButtonStyle”应该具有相同的父名称。您应该更具体地说明
yourItem
,因为“MenuItem.setLayoutParams”不起作用!
android:icon="@drawable/shape_green_rect"
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>

<style name="MyActionButtonStyle" parent="AppBaseTheme">
    <item name="android:minWidth">20dip</item>
    <item name="android:padding">0dip</item>
</style>