简体   繁体   English

如何自动缩放Android工具栏菜单图标?

[英]How to autoscale Android toolbar menu icons?

In my Android activity I've attempted to add a menu icon to a toolbar thus: 在我的Android活动中,我尝试将菜单图标添加到工具栏,因此:

toolbar.inflateMenu(R.menu.toolbar_menu)

The menu is defined using an Android vector drawable: 菜单是使用Android向量drawable定义的:

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

</menu>

The drawable: 可绘制对象:

<!-- drawable/menu.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="24dp"
    android:width="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path android:fillColor="#000" android:pathData="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" />
</vector>

But in the emulator (Nexus 5X on the left, Nexus 10 on the right) the menu icon doesn't autoscale as I would expect: 但是在仿真器中(左侧为Nexus 5X,右侧为Nexus 10),菜单图标不会像我期望的那样自动缩放:

在此处输入图片说明

This occurs under API 21 and API 26. 这在API 21和API 26下发生。

I thought using vector drawables was supposed to lead to auto-scaled images, but it's clearly not occurring here. 我以为使用矢量可绘制对象应该可以生成自动缩放的图像,但是显然这里没有发生。 How can I get this menu to scale properly without resorting to per-resolution raster images? 如何在不求助于每分辨率光栅图像的情况下使此菜单正确缩放?

Try adding android:scaleType="fitXY" ? 尝试添加android:scaleType="fitXY"吗?

Similar issue here 这里类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM