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

Ani*_*l M 15 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: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"/>
Run Code Online (Sandbox Code Playgroud)

Java文件:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_store_locator, menu);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*l M 36

找到解决方案,在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>
Run Code Online (Sandbox Code Playgroud)

  • 如何在菜单项中使用此样式?如果您共享简单的代码,将会很有帮助,谢谢 (2认同)

Md *_*fur 8

创建drawable xml.喜欢res/drawable/shape_green_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>
Run Code Online (Sandbox Code Playgroud)

添加此drawable in

android:icon="@drawable/shape_green_rect"
Run Code Online (Sandbox Code Playgroud)

这样我们就可以在菜单中添加填充.

谢谢.