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

我这里有一个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中设置了drawablleft。

我想在代码中改变可绘制的。

有办法做这件事吗?或者在文本视图的代码中设置drawablleft ?

223939 次浏览

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

你可以使用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属性,但不知道如何在运行时使用它。只需在开发人员文档中查看该属性的描述即可。如果运行时支持相关的方法,你会在那里找到它。例如对于DrawableLeft

你可以使用以下任何方法来设置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);
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;
}

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

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

有两种方法,你可以使用XML或Java。 如果它是静态的,不需要更改,那么可以在XML中初始化。< / p >

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

现在如果你需要动态更改图标,那么你可以通过 根据事件

调用图标
       textViewContext.setText("File Uploaded");
textViewContext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uploaded, 0, 0, 0);

使用芬兰湾的科特林:

你可以创建一个扩展函数或者直接使用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)
}

工作为我改变文本视图左/右可绘制的颜色

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

我这样用。

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