是否可以以编程方式设置drawableLeft可见性

Dav*_*vid 8 android textview android-drawable

我通过以下方式以编程方式添加/删除drawableLeft:

((TextView)view).setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_icon, 0, 0, 0); 
((TextView)view).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 
Run Code Online (Sandbox Code Playgroud)

但是因为我在列表中使用它,

我需要选择删除未选定行的drawableLeft而没有对齐问题.

我想要的是:

  AAA
* BBB
  CCC  
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

AAA
* BBB
CCC  
Run Code Online (Sandbox Code Playgroud)

我可以通过添加透明图标来解决它,

但是我能以编程方式控制这个drawableLeft可见性吗?

Shi*_*yUK 2

我不确定您将可绘制对象应用到哪种类型的视图,但您可以简单地设置左侧填充以匹配可绘制对象不存在时的宽度。

View.setPadding(left, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)