Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何替换”;android:drawableLeft“;CheckedTextView中的图像_Android_Android Layout_Checkedtextview - Fatal编程技术网

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

“如何替换”;android:drawableLeft“;CheckedTextView中的图像,android,android-layout,checkedtextview,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" an

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

但这并不能取代使用过的图像。而是将其添加到同一行。我应该如何用新图像替换当前图像

在您正在使用的xml中

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

((CheckedTextView) convertView).setCompoundDrawables( img, null, null, null );
`android:drawableLeft="@drawable/ic_launcher"`
在适配器中,您正在做什么

((CheckedTextView) convertView).setCheckMarkDrawable(R.drawable.ic_folder);
这是CheckedTextView的两个不同功能。复选标记drawable和drawable是不同的drawable。要添加或更改CheckedTextView的左、右、上、下图像,应使用

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


并更改复选标记drawable
CheckedTextView.setCheckMarkDrawable(R.drawable.ic_文件夹)

这是我作为问题的解决方案所做的。希望它能帮助任何面临同样问题的人。这里的parent是ViewGroup参数

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

@谢谢你的回复。在适配器类(通过BaseExpandableListAdapter类扩展)内使用getContext方法时出现问题。我试图使用这个“Drawable icon\u imageDrawable=parent.getContext().getResources().getDrawable((R.Drawable.ic_文件夹);”但是无法访问Drawable文件夹中的我的图像。你有什么解决方案吗?@vipulmittal我可以解决“Drawable icon\u imageDrawable=parent.getContext().getResources().getDrawable”的问题((R.drawable.ic_folder));“但它不能替换图像。我也找到了原因。我们必须为可绘制设置边界。顺便说一句,你的答案对我自己驾驶到正确的路径很有用。所以不要担心,我会投票:)@我很高兴能提供帮助。你可以编辑你的问题并发布你使用的最终解决方案,也许对其他人有帮助。@vipulmittal好的,我会的。耶!你是对的!我尝试了SetCompoundDrawablesWithinInstincBounds方法,它也可以替换图像。但是我无法在这里管理图像的大小。所以我使用了setCompoundDrawables方法。但是我们的答案也可以这样做。我会投票给你
CheckedTextView.setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)
Drawable imageDrawable=parent.getContext().getResources().getDrawable((R.drawable.ic_folder));
 imageDrawable.setBounds(0,0, 40, 40);
((CheckedTextView) convertView).setCompoundDrawables(imageDrawable, null,null, null);