2013-03-28 67 views
13

enter image description here如何在android中的菜單項之間添加填充?

我有菜單項從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"/> 

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

回答

30

找到解決方案,在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> 
+1

重要的是「minWidth」默認是80dp。這意味着即使沒有填充,也可能有一些空間。這是我的問題。 – 2014-10-02 13:50:45

+1

我如何在我的菜單項中使用這種風格?如果你分享簡單的代碼,這將是有益的 謝謝 – 2017-01-21 08:46:37

-1

填充我不知道,不過你可以通過下面的方法添加保證金:

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

套裝你的價值取而代之f「左,上,右,下」。在這個例子中,我將在您使用ID獲取的商品上添加保證金。

希望這會有所幫助。

+1

因爲'MenuItem.setLayoutParams'不起作用,所以你應該對'yourItem'更具體一些! – msysmilu 2015-02-10 17:53:20

8

create 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> 

加入這個可繪製在

android:icon="@drawable/shape_green_rect" 

這樣我們就可以能夠在菜單中添加填充。

謝謝。

+0

感謝您的回覆!如果我添加'android:icon =「@ drawable/shape_green_rect」'我可以在xml文件中添加我的圖標嗎? – 2013-03-28 09:57:03