340

我在这里有一个 xml 中的 textView。

<TextView
        android:id="@+id/bookTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableLeft="@drawable/checkmark"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="24dip"
        android:maxLines="1"
        android:ellipsize="end"/>

如您所见,我在 xml 中设置了 DrawableLeft。

我想更改代码中的可绘制对象。

无论如何要这样做吗?或者在文本视图的代码中设置 drawableLeft?

4

9 回答 9

908

您可以使用setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

在不需要图像的地方设置 0

左侧可绘制的示例:

TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

或者,您可以使用setCompoundDrawablesRelativeWithIntrinsicBounds来尊重 RTL/LTR 布局。


提示:当您知道任何 XML 属性但不知道如何在运行时使用它时。只需转到开发人员文档中该属性的描述即可。如果在运行时支持,您将在那里找到相关方法。对于 DrawableLeft

于 2011-08-03T19:25:41.320 回答
16

这里我看到方法setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)可以用来做到这一点。

于 2011-08-03T19:25:35.007 回答
14

使用 Kotlin:

您可以创建扩展功能或直接使用setCompoundDrawablesWithIntrinsicBounds

fun TextView.leftDrawable(@DrawableRes id: Int = 0) {
   this.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0)
}

如果你需要调整drawable的大小,你可以使用这个扩展功能。

textView.leftDrawable(R.drawable.my_icon, R.dimen.icon_size)

fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int) {
    val drawable = ContextCompat.getDrawable(context, id)
    val size = resources.getDimensionPixelSize(sizeRes)
    drawable?.setBounds(0, 0, size, size)
    this.setCompoundDrawables(drawable, null, null, null)
}

要获得真正的花哨,请创建一个允许修改大小和/或颜色的包装器。

textView.leftDrawable(R.drawable.my_icon, colorRes = R.color.white)

fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int = 0, @ColorInt color: Int = 0, @ColorRes colorRes: Int = 0) {
    val drawable = drawable(id)
    if (sizeRes != 0) {
        val size = resources.getDimensionPixelSize(sizeRes)
        drawable?.setBounds(0, 0, size, size)
    }
    if (color != 0) {
        drawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
    } else if (colorRes != 0) {
        val colorInt = ContextCompat.getColor(context, colorRes)
        drawable?.setColorFilter(colorInt, PorterDuff.Mode.SRC_ATOP)
    }
    this.setCompoundDrawables(drawable, null, null, null)
}
于 2020-01-08T19:19:44.603 回答
8

您可以使用以下任何一种方法在 TextView 上设置 Drawable:

1- setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)

2- setCompoundDrawables(Left_Drawable、Top_Drawable、Right_Drawable、Bottom_Drawable)

要从资源中获取可绘制性,您可以使用:

getResources().getDrawable(R.drawable.your_drawable_id);
于 2014-04-28T07:23:57.090 回答
1

Kotlin 扩展 + 可绘制对象周围的一些填充

fun TextView.addDrawable(drawable: Int) {
val imgDrawable = ContextCompat.getDrawable(context, drawable)
compoundDrawablePadding = 32
setCompoundDrawablesWithIntrinsicBounds(imgDrawable, null, null, null)
}
于 2019-08-21T05:31:34.153 回答
1

有两种方法可以使用 XML 或 Java。如果它是静态的并且不需要更改,那么您可以在 XML 中进行初始化。

  android:drawableLeft="@drawable/cloud_up"
    android:drawablePadding="5sp"

现在,如果您需要动态更改图标,则可以通过根据事件调用图标来完成

       textViewContext.setText("File Uploaded");
textViewContext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uploaded, 0, 0, 0);
于 2019-11-11T12:45:27.587 回答
1

对我有用,可以更改文本视图左/右可绘制颜色

for (drawable in binding.tvBloodPressure.compoundDrawablesRelative) {
            if (drawable != null) {
                drawable.colorFilter = PorterDuffColorFilter(
                    ContextCompat.getColor(binding.tvBloodPressure.context, color),
                    PorterDuff.Mode.SRC_IN
                )
            }
        }
于 2021-11-03T07:57:55.657 回答
0

我是这样使用的。

  txtUserName.setCompoundDrawablesWithIntrinsicBounds(
        requireActivity().getDrawable(
            R.drawable.ic_my_account
        ), null, null, null
    )
于 2021-12-21T09:55:37.827 回答
-8
static private Drawable **scaleDrawable**(Drawable drawable, int width, int height) {

    int wi = drawable.getIntrinsicWidth();
    int hi = drawable.getIntrinsicHeight();
    int dimDiff = Math.abs(wi - width) - Math.abs(hi - height);
    float scale = (dimDiff > 0) ? width / (float)wi : height /
            (float)hi;
    Rect bounds = new Rect(0, 0, (int)(scale * wi), (int)(scale * hi));
    drawable.setBounds(bounds);
    return drawable;
}
于 2015-07-27T08:21:48.363 回答