how to add padding between menu items in android?

2019-01-09 10:03发布

This question already has an answer here:

enter image description here

I'm having menu items being inflated from res/menu/menu.xml on to ActionBar, how do I add padding between menu items using 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"/>

Java file:

@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;
}

3条回答
Lonely孤独者°
2楼-- · 2019-01-09 10:23

Found solution, added following lines in styles.xml file and it worked!!

<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>
查看更多
Summer. ? 凉城
3楼-- · 2019-01-09 10:37

create drawable xml . like 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>

Add this drawable in

android:icon="@drawable/shape_green_rect"

This way we can able to add padding in menu.

Thanks.

查看更多
Emotional °昔
4楼-- · 2019-01-09 10:41

Padding I don't know, but you can add margin by the following way:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
yourItem.setLayoutParams(lp);

Set your values instead of "left, top, right, bottom". In this example I'm adding margin on an item that you would get with its ID.

Hope this helps.

查看更多
登录 后发表回答