【问题标题】:how to add padding between menu items in android? [duplicate]如何在android中的菜单项之间添加填充? [复制]
【发布时间】:2013-03-18 16:36:30
【问题描述】:

我的菜单项从 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;
}

【问题讨论】:

    标签: android android-layout android-actionbar


    【解决方案1】:

    找到了解决方案,在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>
    

    【讨论】:

    • 重要的是“minWidth”,默认为 80dp。这意味着即使没有填充也可以有一些空间。这就是我的问题。
    • 如何在我的菜单项中使用这种风格??如果您分享简单的代码,将会有所帮助谢谢
    • 我注意到我们的主题样式和“MyActionButtonStyle”应该具有相同的父名称
    【解决方案2】:

    创建可绘制的 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>
    

    中添加这个drawable
    android:icon="@drawable/shape_green_rect"
    

    这样我们就可以在菜单中添加内边距了。

    谢谢。

    【讨论】:

    • 感谢您的回复!!如果我添加 android:icon="@drawable/shape_green_rect" 我应该在 xml 文件中的哪里添加我的图标?
    【解决方案3】:

    填充我不知道,但是你可以通过以下方式添加边距:

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

    设置您的值,而不是“左、上、右、下”。在此示例中,我将在您将通过其 ID 获得的项目上添加边距。

    希望这会有所帮助。

    【讨论】:

    • 您应该更具体地了解yourItem,因为“MenuItem.setLayoutParams”不起作用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多