如何在检查文本视图中替换"android:drawableLeft"图像



i使用" android:dablableleft"设置了布局中的图像图标。这是我的布局代码:

<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"/> 

我需要做的是,使用我的班级将此图像更改为其他东西。这是我在适配器类中使用的代码,该代码与baseexpandablelistadapter扩展。

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

,但这并不能替换使用的图像。取而代之的替换为同一行。我该怎么做才能用新图像替换当前图像?

在您的XML中,您正在使用

`android:drawableLeft="@drawable/ic_launcher"`

,在适配器中,您正在做

((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);

这些是checkedTextView的两个不同功能。可绘制和可绘制的检查标记是不同的可抽签。要添加或更改左右,右,顶部,底部图像 cheptedtextview ,您应该使用

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)

和更改选中标记可绘制的CheckedTextView.setCheckMarkDrawable(R.drawable.ic_folder);

这是我为解决问题的解决方案所做的。希望这将帮助任何面临同样问题的人。在这里,父母是ViewGroup参数,

Drawable imageDrawable=parent.getContext().getResources().getDrawable((R.drawable.ic_folder));
 imageDrawable.setBounds(0,0, 40, 40);
((CheckedTextView) convertView).setCompoundDrawables(imageDrawable, null,null, null);
Drawable img = getContext().getResources().getDrawable( R.drawable.ic_folder );
((CheckedTextView) convertView).setCompoundDrawables( img, null, null, null );

最新更新