设置动作条图标的高度来匹配动作条的高度高度、动作、图标

2023-09-12 23:40:14 作者:攒一口袋星星

看到这里的问题,灰色的复选框,离开房间从操作栏的顶部和底部:

see the problem here, the grey checkbox leaves room from the top and bottom of the action bar:

styles.xml

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomActionBarTheme" parent="Theme.Base.AppCompat.Light">
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:actionButtonStyle">@style/ActionButtonStyle</item>
</style>

<!--<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">-->
<style name="ActionButtonStyle" >
    <item name="android:height">@dimen/abc_action_bar_default_height</item>
    <item name="android:layout_height">@dimen/abc_action_bar_default_height</item>
</style>

我设置的项目,像这样:

i set the item like so:

getMenuInflater().inflate(R.menu.submit_action, menu);

submit_action是这样的:

submit_action looks like:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_submit"
        android:icon="@drawable/check"
        app:showAsAction="always"  />
</menu>

最后,附上了@可绘制/检查正在使用。

and finally, attached is the @drawable/check being used.

任何想法如何,我可以得到检查,以填补动作条?

any idea how i can get the check to fill the actionbar?

推荐答案

你的图标不填写的原因动作条是由于如何使用 ActionMenuItemView 测量图标。

The reason your icon doesn't fill the ActionBar is due to how the ActionMenuItemView measures the icon.

ActionMenuItemView 调用 32dp 的最大尺寸时,设置的边界,你的图标

所以,它使这么差你的形象有多大时,系统会一直调整其大小。

So, it makes so difference how large your image is, the system will always resize it.

任何想法如何,我可以得到检查,以填补动作条?

any idea how i can get the check to fill the actionbar?

您应该使用你的菜单项一个动作的布局,没有背景的复选标记图标,更改操作布局父的背景颜色,你认为合适。这里有一个例子:

You should use a action layout for your MenuItem, a check mark icon with no background, and change the background color of the action layout parent as you see fit. Here's an example:

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/actionButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#e8e8e8"
    android:clickable="true"
    android:contentDescription="@string/cd" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_tick" />

</RelativeLayout>

菜单项

<item
    android:id="@+id/action_submit"
    android:actionLayout="@layout/your_action_layout"
    android:showAsAction="always"/>

辅助功能

重要的是要确保你的菜单项是可访问的。通常情况下,当你长时间preSS中的一个项目的动作条吐司将显示的内容描述你,但是当你使用自定义的菜单项这是由你来实现这个模式。一个简单的方法来做到这一点是使用罗马Nurik的的cheatsheet 。如果你不确定如何使用它,你应该参考这里我的回答的进入更多的细节上创建自定义菜单项的布局。

It's important to make sure your MenuItem is accessible. Normally when you long press an item in the ActionBar a short Toast will display the content description for you, but when you're using a custom MenuItem it's up to you to implement this pattern. An easy way to do this is by using Roman Nurik's CheatSheet. If you're unsure how to use it, you should refer to my answer here that goes into much more detail on creating custom MenuItem layouts.

或者

如果您想要使用的背景颜色,每菜单项你有,你可以创建一个自定义样式,并使用应用它的android:actionButtonStyle 属性。下面是一个例子:

If you want to use that background color for every MenuItem you have, you could create a custom style and apply it using the android:actionButtonStyle attribute. Here's an example of that:

您的风格

<style name="Your.ActionButton" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:background">#e8e8e8</item>
</style>

您的主题

<style name="Your.Theme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionButtonStyle">@style/Your.ActionButton</item>
</style>

结果