如何替换 CheckedTextView 中的“android:drawableLeft”图像

IBu*_*nny 3 android checkedtextview android-layout

我使用“android:drawableLeft”在布局中设置图像图标。这是我的布局代码:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_marginLeft="8dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:gravity="left"/> 
Run Code Online (Sandbox Code Playgroud)

我需要做的是,使用我的课程将此图像更改为其他图像。这是我在使用 BaseExpandableListAdapter 扩展的 Adapter 类中使用的代码。

if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);
Run Code Online (Sandbox Code Playgroud)

但这不是替换使用过的图像。相反,替换它被添加到同一行。我应该怎么做才能用新图像替换当前图像?

小智 5

在您使用的 xml 中

`android:drawableLeft="@drawable/ic_launcher"`
Run Code Online (Sandbox Code Playgroud)

在适配器中你正在做

((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);
Run Code Online (Sandbox Code Playgroud)

这是 CheckedTextView 的两个不同功能。CheckMark drawable 和 drawable 是不同的 drawable。要添加或更改CheckedTextView 的左、右、上、下图像,您应该使用

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)
Run Code Online (Sandbox Code Playgroud)

或者

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)
Run Code Online (Sandbox Code Playgroud)

并更改 CheckMark drawable CheckedTextView.setCheckMarkDrawable(R.drawable.ic_folder);