2013-03-28 9 views
13

enter image description hereWie füge ich Padding zwischen Menüelementen in Android hinzu?

Ich habe Menüpunkte aus res/Menü/menu.xml auf ActionBar aufgeblasen wird, wie füge ich Polsterung zwischen den Menüpunkten mit 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-Datei:

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

Antwort

30

Gefunden Lösung, folgende Zeilen in styles.xml Datei hinzugefügt und es hat funktioniert !!

<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> 
+1

Die wichtige Sache ist "minWidth" es ist 80dp standardmäßig. Das heißt, auch ohne Polsterung kann Platz sein. Das war das Problem in meinem Fall. –

+1

Wie kann ich diesen Stil in meinem Menüpunkt verwenden? Wenn Sie einfachen Code teilen, wird es hilfreich sein Danke –

-1

Padding Ich weiß es nicht, aber man kann durch die folgende Weise Rand hinzu:

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

Set deine Werte stattdessen o f "links, oben, rechts, unten". In diesem Beispiel füge ich einen Rand zu einem Artikel hinzu, den du mit seiner ID erhalten würdest.

Hoffe, das hilft.

+1

Sie sollten genauer auf 'yourItem' sein, weil 'MenuItem.setLayoutParams' nicht funktioniert! – msysmilu

8

erstellen Sie ausklappbare XML. wie 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> 

dieses ziehbar hinzufügen in

android:icon="@drawable/shape_green_rect" 

Auf diese Weise können wir padding in Menü hinzufügen können.

Danke.

+0

Danke für die Antwort !! Wenn ich 'android: icon =" @ drawable/shape_green_rect "' hinzufüge, wo füge ich meine Icons in die XML-Datei ein? –

Verwandte Themen