2013-03-28 13 views
13

enter image description hereアンドロイドのメニュー項目間にパディングを追加する方法は?

私はアンドロイドを使用してメニュー項目間のパディングを追加するにはどうすればよい、アクションバーの上のres /メニュー/ menu.xmlから膨張するメニュー項目を持っていますか?

<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です。つまり、パディングなしでさえ、いくらかのスペースがあることを意味します。それが私の場合の問題でした。 –

+1

メニューアイテムにこのスタイルを使用するにはどうすればいいですか?シンプルなコードを共有すると便利です ありがとう –

-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

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ファイルにアイコンを追加する場所はどこですか? –

関連する問題