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

时间:2014-01-23 06:51:02

标签: android android-layout checkedtextview

我使用“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"/> 

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

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

但这不是替换使用过的图像。而是替换它添加到同一行。如何用新的图像替换当前图像?

3 个答案:

答案 0 :(得分:4)

在你正在使用的xml中

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

在你正在做的适配器中

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

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

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

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

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

答案 1 :(得分:1)

这就是我作为问题的解决方案所做的。希望它能帮助任何遇到同样问题的人。这里的父是ViewGroup参数,

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

答案 2 :(得分:0)

Drawable img = getContext().getResources().getDrawable( R.drawable.ic_folder );

((CheckedTextView) convertView).setCompoundDrawables( img, null, null, null );