我这里有一个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?
您可以使用 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
您可以创建扩展函数或直接使用
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)
}
从这里我看到方法setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)可以用来做到这一点。
您可以使用以下任意一种方法在 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);
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 中进行初始化。
android:drawableLeft="@drawable/cloud_up"
android:drawablePadding="5sp"
现在如果您需要动态更改图标,那么您可以通过 根据事件调用图标
textViewContext.setText("File Uploaded");
textViewContext.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uploaded, 0, 0, 0);
适用于我更改文本视图左/右可绘制颜色
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
)
上、下、左、右为图标资源。
TextView textView = (TextView) findViewById(R.id.your_id);
textView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
// for example
personalTextView = (TextView) findViewById(R.id.personal_info);
personalTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.personal, 0, 0, 0);
在 Android 中,您可以使用 setCompoundDrawablesRelativeWithIntrinsicBounds 方法以编程方式设置 TextView 的左侧可绘制对象(也称为复合可绘制对象)。这是 Java 中的示例:
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Assume you have a TextView with the id "textView"
TextView textView = findViewById(R.id.textView);
// Set the left drawable programmatically
Drawable drawable = getResources().getDrawable(R.drawable.your_drawable); // Replace with your drawable resource
setLeftDrawable(textView, drawable);
}
private void setLeftDrawable(TextView textView, Drawable drawable) {
// Set the left drawable with intrinsic bounds
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
}
}
将 R.drawable.your_drawable 替换为您的可绘制对象的实际资源 ID。此示例假设您的布局 XML 文件中有一个 ID 为 textView 的 TextView。
请记住处理可绘制对象或 TextView 可能为 null 的情况,具体取决于您的应用程序逻辑。
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;
}