【问题标题】:Android VectorDrawable as Compound DrawablesAndroid VectorDrawable 作为复合 Drawable
【发布时间】:2016-05-10 07:13:56
【问题描述】:

this android developer blog post 所述,我们现在可以使用AppCompat 23.2.0 及更高版本在Android API 7+ 上使用VectorDrawables

对我来说一切似乎都很好,除了将可绘制对象用作 TextView 的复合。

通常情况下,人们会这样做:

            customTab.setCompoundDrawablesWithIntrinsicBounds(
                0,
                R.drawable.my_vector,
                0,
                0
            );

不幸的是,目前这不起作用,我无法找到解决此问题的方法。

如帖子所述,唯一可用且有效的方法是 xml 方法,使用 app:srcCompat="@drawable/..." 和 Java setImageResource(...)

如何通过setCompoundDrawable() 方法使用新的矢量可绘制支持?

提前致谢。

编辑:

根据要求,这是VectorDrawableCompat 类的结果:

xml 是:

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

<path
    android:fillColor="#4D4D4D"
    android:strokeWidth="2"
    android:strokeColor="#4D4D4D"
    android:pathData="M63.3336386,72.2631001 C56.7778507,76.9021242
48.7563953,79.6307404
40.09319,79.6307404 C17.9503315,79.6307404 0,61.804793 0,39.8153702
C0,17.8259473 17.9503315,0 40.09319,0 C62.2360484,0 80.1863799,17.8259473
80.1863799,39.8153702 C80.1863799,50.8100816 75.6987973,60.7639242
68.4433567,67.9690887 L96.7320074,96.0617174 C98.0293966,97.3501165
97.9978616,99.4159703 96.6953405,100.709466 C95.3837385,102.011979
93.2974318,102.019264 92.0151615,100.745879 L63.3336386,72.2631001
L63.3336386,72.2631001 L63.3336386,72.2631001 Z M40.09319,74.9465792
C59.6310061,74.9465792 75.4695341,59.217802 75.4695341,39.8153702
C75.4695341,20.4129383 59.6310061,4.6841612 40.09319,4.6841612
C20.5553738,4.6841612 4.71684588,20.4129383 4.71684588,39.8153702
C4.71684588,59.217802 20.5553738,74.9465792 40.09319,74.9465792
L40.09319,74.9465792 L40.09319,74.9465792 Z" />
</vector>

【问题讨论】:

  • 使用VectorDrawableCompat#create并在调用setCompoundDrawablesWithIntrinsicBounds时使用返回的Drawable
  • @pskink 这似乎工作,至少在 6.0,但实际可绘制重叠文本,知道吗?
  • 不,复合drawables不应与内容重叠,发布图像和您的定义
  • @pskink 查看编辑,此外,该图标在 Android 4.4.4 上仍然不可见
  • this 已在 4.4.4 上测试并正常工作

标签: android android-support-library android-vectordrawable


【解决方案1】:

support library 23.2开始可以使用下一个解决方案:

Drawable drawable=AppCompatDrawableManager.get().getDrawable(mContext, R.drawable.drawable);
view.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);

【讨论】:

  • Android Studio 显示此警告:“AppCompatDrawableManager 只能从同一个库组中调用 (groupId=com.android.support)”。我认为替代方案是:AppCompatResources#getDrawable
  • 我相信首选的选择是ContextCompat.getDrawable(mContext, R.drawable.drawable)
【解决方案2】:

按照@pskink 给出的宝贵建议,我能够在我的视图中正确加载一个drawable。

我的问题是我在TabLayout 中将选择器用作我的 xml 以提供“当前活动选项卡”反馈。

我通过演员解决了我的问题:

Drawable drawable;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawable = ContextCompat.getDrawable(mContext, tabIcons[i]);
        } else {
            drawable = getResources().getDrawable(tabIcons[i]);
        }

        StateListDrawable stateListDrawable =  (StateListDrawable) drawable;
        customTab.setCompoundDrawablesWithIntrinsicBounds(
                null,
                stateListDrawable,
                null,
                null
        );

【讨论】:

    【解决方案3】:

    你也可以通过数据绑定来解决:

    创建适配器方法

    public class Bindings {
    @BindingAdapter({"bind:drawableStartId"})
        public static void setDrawableStart(TextView textView, @DrawableRes int id) {
            Drawable drawable = AppCompatDrawableManager.get().getDrawable(textView.getContext(), id);
            Drawable drawables[] = textView.getCompoundDrawablesRelative();
            textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, drawables[1], drawables[2], drawables[3]);
        }
    }
    

    并在您的 xml 文件中使用 app:drawableStartId

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <data>
            <import type="your.path.to.R" />
        </data>
        <TextView
        app:drawableStartId="@{isSelected ? R.drawable.one :R.drawable.another}"
        />
    <layout/>
    

    【讨论】:

    • 谢谢,这看起来很有趣!我会做进一步的测试,让你知道它的适合度
    • @Matteo 它就像流行的字体绑定库github.com/lisawray/fontbinding
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2012-02-11
    相关资源
    最近更新 更多