如何在Android工具栏上使用VectorDrawable? [英] How can I use VectorDrawable with the Android Toolbar?

查看:121
本文介绍了如何在Android工具栏上使用VectorDrawable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工具栏中使用新的VectorDrawable的正确方法是什么?

What is the proper method to use the new VectorDrawable in the toolbar?

我尝试使用app:srcCompat元素,如下所示,但未显示任何内容.

I tried to use the app:srcCompat element as illustrated below, but nothing showed up.

<menu
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
  <item
    app:srcCompat="@drawable/ic_clear"
    app:showAsAction="ifRoom" />
</menu>

我在JB(16)上使用android.support.v7.widget.Toolbar和Android支持库v23.2有自己的工具栏布局.

I have my own toolbar layout using android.support.v7.widget.Toolbar and Android Support Library v23.2 on JB (16).

推荐答案

结果很简单. 假设您有矢量可绘制vd_trash_24dp.

Turns out it's quite easy. Say, you have vector drawable vd_trash_24dp.

描述MenuItem不能直接使用android:icon解决VectorDrawable.似乎也忽略了app:srcCompat.

Describing MenuItem one cannot address VectorDrawable directly with android:icon. It seems ignoring app:srcCompat also.

但是.作为我们所知道的;)

AppCompat确实支持在加载矢量可绘制对象时进行加载 在另一个可绘制容器(例如StateListDrawable)中引用, InsetDrawable,LayerDrawable,LevelListDrawable和RotateDrawable

AppCompat does support loading vector drawables when they are referenced in another drawable container such as a StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable

让我们尝试一下吧?

创建StateListDrawable vd_test_vd

Create StateListDrawable vd_test_vd

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/vd_trash_24dp" />

</selector>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools">
    <item android:id="@+id/menu_action_filter"
          android:title="@string/menu_action_filter"
          android:icon="@drawable/vd_test_vd"
          android:orderInCategory="100"
          app:showAsAction="always"/>
</menu>

真正的街头魔术.

是的,您可以尝试在运行时使用MenuItem.setIcon()设置可绘制.但是谁需要那个%)

Yes, one could try and set drawable at runtime with MenuItem.setIcon(). But who need that %)

这篇关于如何在Android工具栏上使用VectorDrawable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆