gpt4 book ai didi

android - 除了带有 srcCompat 的 ImageView 之外,如何使用带有 View 的矢量可绘制对象?

转载 作者:IT王子 更新时间:2023-10-29 00:03:18 26 4
gpt4 key购买 nike

app:srcCompatImageView 允许向后兼容使用矢量可绘制对象。但是除了 ImageView 之外,你如何将它们与其他 View 一起使用呢?例如,TextView 属性如 android:drawableLeft.

同时使用矢量可绘制对象作为 android:iconMenuItem 会导致崩溃,并出现以下异常:

Fatal Exception: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:626)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.support.v7.view.menu.MenuItemImpl.setActionView(MenuItemImpl.java:621)
at android.support.v7.view.menu.MenuItemImpl.setActionView(MenuItemImpl.java:40)
at android.support.v4.view.MenuItemCompat.setActionView(MenuItemCompat.java:310)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:465)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at com.example.niceapp.context.main.MainActivity.onCreateOptionsMenu(MainActivity.java:101)
at android.app.Activity.onCreatePanelMenu(Activity.java:2578)

使用支持库 23.2.0,如何解决此问题?

最佳答案

更新 2:他们在支持库 23.4.0 中添加了一个选项以再次启用它:

For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled() - keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default.

更新:从 23.3.0 版开始,这不再有效

For AppCompat users, we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1 [https://code.google.com/p/android/issues/detail?id=205236, https://code.google.com/p/android/issues/detail?id=204708]. Using app:srcCompat and setImageResource() continues to work.

来自 Android Developers Google+ post


Using AppCompat and app:srcCompat is the most foolproof method of integrating vector drawables into your app.

该引用来自官方blogpost用于支持库 23.2.0 版的发布。

该帖子还提到以下内容:

You’ll find directly referencing vector drawables outside of app:srcCompat will fail prior to Lollipop. However, AppCompat does support loading vector drawables when they are referenced in another drawable container such as a StateListDrawable, InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable. By using this indirection, you can use vector drawables in cases such as TextView’s android:drawableLeft attribute, which wouldn’t normally be able to support vector drawables.

这转化为以下步骤:

第 1 步:

创建或导入应用所需的矢量资源。例如,可以为搜索图标创建一个矢量可绘制对象并将其命名为 ic_action_search_vector.xml

第 2 步:

为之前创建的矢量可绘制资源创建另一个代理可绘制资源。比如说,对于前面的 ic_action_search_vector.xmlic_action_search.xml 可以创建为一个简单的 StateListDrawable,其中可以包含以下行:

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

如果您从另一个将与您的 View 一起使用的可绘制资源中引用了可绘制矢量,则可以跳过此步骤。

第 3 步:

使用引用矢量可绘制对象 (ic_action_search_vector.xml) 的可绘制资源(此处为 ic_action_search.xml),而不是直接使用可绘制矢量。对于菜单,它看起来像:

<item android:id="@+id/search"
android:title="@string/search"
android:icon="@drawable/ic_action_search"
app:showAsAction="always"/>

这就是解决这个问题的方法!

关于android - 除了带有 srcCompat 的 ImageView 之外,如何使用带有 View 的矢量可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35800334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com