【问题标题】:Remove the Padding for Action Bar Menu Item using AppCompat使用 AppCompat 删除操作栏菜单项的填充
【发布时间】:2013-10-01 05:59:37
【问题描述】:

我正在通过对我的活动使用以下样式来使用覆盖操作栏

style.xml

    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat" >
    <item name="windowActionBarOverlay">true</item>
    <item name="android:background">#00f3ead8</item>
    <item name="android:windowBackground">@drawable/logo1024</item>
</style>

我只有一个要始终显示的菜单项

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/menusettings"
    android:orderInCategory="100"
    android:icon="@drawable/settings"
    android:title="Menu"
    yourapp:showAsAction="always"
    />
  <menu>

图标,即 settings.png 被 8dp 填充 但我希望在顶部和底部填充 0 以覆盖整个操作栏

请帮助我在接近项目截止日期的时候陷入困境 提前致谢

【问题讨论】:

  • 我在 menu.xml 文件的 标记中尝试过 android:paddingTop="0dp" 和 android:paddingBottom="0dp" 但没有用
  • 您可能需要考虑使用自定义android:actionLayout 来可视化操作项。这将允许您完全控制其样式,包括填充。 See here for an example.
  • @MH 我正在使用 Appcompat 操作栏 我应该使用什么样式?对于 actionLayout 样式
  • 我已经根据您的链接修改了代码,如下所示
  • 我已经根据你的链接修改了代码,如下code 和 dis code 但不工作!!

标签: android android-actionbar android-appcompat


【解决方案1】:

一年后... 删除 v7.Toolbar 的操作按钮上的填充仍然具有挑战性。 经过大量实验,这里是一个可行的解决方案,在 API 15 及更高版本上进行了测试(理论上应该可以工作 > API 7)。 首先,工具栏样式(主题)应该这样应用

<android.support.v7.widget.Toolbar
     app:theme="@style/AppTheme.Toolbar"
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>

接下来,values/styles.xml

<style name="AppTheme.Toolbar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="actionButtonStyle">@style/AppTheme.Toolbar.ActionButton</item>
</style>
<!-- this will be applied regardless api version -->
<style name="Base.AppTheme.Toolbar.ActionButton" parent="@style/Widget.AppCompat.ActionButton">
    <item name="android:minWidth">0dp</item>
    <item name="android:minHeight">0dp</item>
</style>

<!-- this will be applied on < API 17 -->
<style name="AppTheme.Toolbar.ActionButton" parent="@style/Base.AppTheme.Toolbar.ActionButton">
    <item name="android:padding">0dp</item>
</style>

最后一步,values-v17/styles.xml

<!-- this will be applied on >= API 17 -->
<style name="AppTheme.Toolbar.ActionButton" parent="@style/Base.AppTheme.Toolbar.ActionButton">
    <item name="android:paddingStart">0dp</item>
    <item name="android:paddingEnd">0dp</item>
</style>

【讨论】:

    【解决方案2】:

    嗯,您的问题是大约一年前提出的,但它可能对其他人有所帮助。

    在此处查看解决方案: how to add padding between menu items in android?

    单独设置填充可能无济于事。您也必须使用 minWidth 属性。 我也在使用 AppCompat,绝对有效。

    <style name="Theme.Styled" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="actionButtonStyle">@style/Widget.Styled.Base.ActionButton</item>
    </style>
    
    <style name="Widget.Styled.Base.ActionButton" parent="@style/Widget.AppCompat.Base.ActionButton">
        <item name="android:minWidth">20dp</item>
        <item name="android:paddingLeft">0dp</item>
        <item name="android:paddingRight">15dp</item>
    </style>
    

    【讨论】:

    • 我正在尝试减少菜单项之间的填充,并且我正在尝试使用相同的代码,但它没有任何效果!有什么想法吗?
    • 我不知道如何减少项目之间的规范。上面的代码适用于我工作的不同项目的 4 个。我不知道为什么不适合你动态顶栏库。这样生活更美好。
    猜你喜欢
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    相关资源
    最近更新 更多