【问题标题】:How to have a selector of vector drawables?如何拥有矢量可绘制对象的选择器?
【发布时间】:2016-04-10 09:35:51
【问题描述】:

背景

我制作了以下 ImageView,以支持选择器作为“src”:

public class CheckableImageView extends ImageView implements Checkable {
    private boolean mChecked;

    private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };

    public CheckableImageView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.com_app_CheckableImageView, 0, 0);
        setChecked(a.getBoolean(R.styleable.com_app_CheckableImageView_com_app_checked, false));
        a.recycle();
    }

    @Override
    public int[] onCreateDrawableState(final int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked())
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        return drawableState;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    public interface OnCheckStateListener {
        void onCheckStateChanged(boolean checked);
    }

    private OnCheckStateListener mOnCheckStateListener;

    public void setOnCheckStateListener(OnCheckStateListener onCheckStateListener) {
        mOnCheckStateListener = onCheckStateListener;
    }

    @Override
    public void setChecked(final boolean checked) {
        if (mChecked == checked)
            return;
        mChecked = checked;
        refreshDrawableState();
        if (mOnCheckStateListener != null) 
            mOnCheckStateListener.onCheckStateChanged(checked);
    }
}

问题

上述代码适用于普通选择器,这些选择器将图像文件作为每个状态的可绘制项目。

问题是,它根本不适用于矢量可绘制对象(使用“srcCompat”)。相反,它显示的是一个空的内容。

这是我尝试过的:

        <...CheckableImageView
         ...
            app:srcCompat="@drawable/selector"/>

而选择器(例如)是:

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

示例矢量可绘制:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="48dp"
        android:viewportWidth="48"
        android:viewportHeight="48">

    <path
        android:fillColor="#0000ff"
        android:strokeColor="#000000"
        android:pathData="M 0 0 H 48 V 48 H 0 V 0 Z" />

    <path
        android:fillColor="#ff0000"
        android:strokeColor="#000000"
        android:pathData="M14.769224,32.692291l5.707315,-17.692275l3.073244,17.479182l6.585245,-16.413424l2.634209,16.200186l-4.170761,-8.526356l-5.048693,7.247362l-5.268419,-8.100027l-3.51214,9.805351z" />
</vector>

问题

为什么它不起作用?我做的有什么问题?我该如何解决?

【问题讨论】:

标签: android android-selector android-vectordrawable


【解决方案1】:

看起来,这是支持库工作方式中的一个错误,并且没有以任何方式记录。

我已尝试发布有关它的错误报告,但 Google 将其标记为 UserError,即使我没有看到它记录在案或有任何警告:

按预期工作。容器中不支持向量,除非 你打开 AppComaptDelegate.setCompatVectorFromResourcesEnabled(true)。

https://code.google.com/p/android/issues/detail?id=210745

因此,如果您看到未显示的选择器,或者导致此日志崩溃:

原因:android.content.res.Resources$NotFoundException:文件 res/drawable/selector.xml 来自可绘制资源 ID #0x7f02004f

您应该避免在选择器中使用vectorDrawable,或者避免使用vectorDrawables.useSupportLibrary=true 这一行。

您可以使用 AppComaptDelegate.setCompatVectorFromResourcesEnabled(true) ,但是according to the docs,这可能是错误的(主要是内存/性能问题),根本不建议使用:

设置是否可以在旧平台 (

启用后,AppCompat 可以从 框架,它可以在内部对矢量可绘制对象进行隐式膨胀 DrawableContainer 资源。然后,您可以在 ImageView 上的 android:src 或 android:drawableLeft 等位置 文本视图。

此功能默认为禁用,因为启用它可能会导致问题 内存使用情况,以及更新配置实例的问题。如果 您手动更新配置,那么您可能不希望 启用此功能。您已收到警告。

即使禁用此功能,您仍然可以通过以下方式使用矢量资源 setImageResource(int) 和它的 app:srcCompat 属性。他们还可以 可用于 AppComapt 为您扩展的任何内容,例如菜单 资源。

请注意:这仅在此后创建的活动中生效 打电话。

【讨论】:

    【解决方案2】:

    我只是通过使用以下 XML 实现的

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/ic_icons8_download_from_the_cloudwhite" android:state_focused="false" android:state_pressed="false" />
      <item android:drawable="@drawable/ic_download_from_the_cloudclicked" android:state_focused="true" android:state_pressed="true" />
      <item android:drawable="@drawable/ic_download_from_the_cloudclicked" android:state_focused="false" android:state_pressed="true" />
      <item android:drawable="@drawable/ic_download_from_the_cloudclicked" android:state_focused="false" />
    </selector>
    

    希望这对遇到同样问题的人有所帮助

    【讨论】:

      【解决方案3】:

      只需创建带有更改颜色的 xml。并在运行时为按钮设置可绘制的选定颜色。

      【讨论】:

      • 这与问题无关。选择器可以做的远不止这些。
      猜你喜欢
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 2019-03-28
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      相关资源
      最近更新 更多