以编程方式在 TextView 中设置左侧可绘制对象

358
我在这里有一个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?

9个回答

963

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

将不需要的图像参数设置为0

左侧Drawable的示例:

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

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


提示:每当您了解任何XML属性但不知道如何在运行时使用它时,请转到开发人员文档中该属性的描述。如果它在运行时受支持,则会找到Related Methods,例如对于DrawableLeft


2
+1 在程序中动态设置 TextView 的 android:drawableLeft 属性是可以工作的。谢谢朋友。 - Paresh Mayani
47
加上小费赞一个。这应该是开发人员使用文档时首先要告诉他们的事情。 - gmjordan
@BrainCrash 你说得对,我把它和同名的新方法搞混了。 - deathemperor
如何使用直接可绘制?还有其他方法吗?我有位图对象,如何设置像这样。@BrainCrash - Pratik Butani
2
你好,我之前使用了这个方法来设置drawable,但是我找不到它的getter方法。我需要检查Compound TextView是否有一些drawable。我该怎么做呢?我尝试将textview.getCompoundDrawablesRelative()[0]mContext.getResources().getDrawable(R.drawable.my_drawable)进行比较。 - ravi
显示剩余4条评论

20

使用Kotlin:

您可以创建扩展函数,也可以直接使用setCompoundDrawablesWithIntrinsicBounds

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

如果您需要调整可绘图的大小,可以使用此扩展函数。

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)
}

17

8

不是的,setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)是来自您自己给出的链接中的API Level 3... - BrainCrash

5

一个Kotlin扩展函数 + 在可绘制物周围添加一些填充

fun TextView.addLeftDrawable(drawable: Int, padding: Int = 32) {
    val imgDrawable = ContextCompat.getDrawable(context, drawable)
    compoundDrawablePadding = padding
    setCompoundDrawablesWithIntrinsicBounds(imgDrawable, null, null, null)
}

1
稍微不同的解决方案:fun TextView.setDrawables(@DrawableRes start: Int = 0, @DrawableRes top: Int = 0, @DrawableRes end: Int = 0, @DrawableRes bottom: Int = 0) { setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom) } - kitfist0

2

有两种方法可以实现,一种是使用XML,另一种是使用Java。 如果它是静态的且不需要更改,则可以在XML中初始化。

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

如果您需要动态更改图标,则可以通过基于事件调用图标来实现。

       textViewContext.setText("File Uploaded");
textViewContext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uploaded, 0, 0, 0);

1

对我来说,更改文本视图左/右可绘制颜色是有效的

for (drawable in binding.tvBloodPressure.compoundDrawablesRelative) {
            if (drawable != null) {
                drawable.colorFilter = PorterDuffColorFilter(
                    ContextCompat.getColor(binding.tvBloodPressure.context, color),
                    PorterDuff.Mode.SRC_IN
                )
            }
        }

0

我是这样使用的。

  txtUserName.setCompoundDrawablesWithIntrinsicBounds(
        requireActivity().getDrawable(
            R.drawable.ic_my_account
        ), null, null, null
    )

-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;
}

这并没有回答问题。预期的是与TextView相关的答案。 - Rick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接