调用setCompoundDrawables()
方法不会显示Compound Drawable,可能是因为在调用该方法之前,没有正确设置Compound Drawable的资源。Compound Drawable是一种在TextView或Button等文本控件中显示图像的方法,它可以在文本的上、下、左、右四个方向上显示图像。
在Android中,可以使用setCompoundDrawables()
方法来设置Compound Drawable。该方法有两种重载形式:
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom);
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);
在调用setCompoundDrawables()
方法之前,需要先创建一个Drawable资源,并将其作为参数传递给该方法。例如,可以使用以下代码来创建一个Drawable资源:
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.your_drawable);
然后,将创建的Drawable资源作为参数传递给setCompoundDrawables()
方法:
textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
这样就可以在TextView的左侧显示Compound Drawable了。
需要注意的是,如果在调用setCompoundDrawables()
方法之前没有正确设置Compound Drawable的资源,那么在运行时就不会显示Compound Drawable。此外,如果Compound Drawable的大小超过了文本控件的大小,也可能会导致Compound Drawable无法显示。因此,在使用Compound Drawable时,需要确保正确设置了资源和控件大小。
推荐的腾讯云相关产品:
产品介绍链接地址:
没有搜到相关的文章