安卓:如何设置可见的drawable

13

这是我的代码:

<TextView
                android:id="@+id/error_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                android:drawablePadding="5dp"
                android:gravity="center"
                android:drawableRight="@drawable/error_image"
                android:textColor="#aa0000"
                android:textStyle="bold"
                android:textSize="16dp"/>

我有一个可绘制对象,我想将其设置为不可见状态,并且当出现错误时再显示该可绘制对象。我该如何做?


2
在XML文件中使用android:visibility="invisible",在Java类中使用TextView.setVisibility(View.VISIBLE)。 - Ravneet Singh
1
你可以在TextView中使用setError方法。具体请参考:http://developer.android.com/reference/android/widget/TextView.html#setError(java.lang.CharSequence)。 - Raghunandan
@ravneet:我知道这个,但我认为应该用另一种方式来做,不过它已经起作用了。谢谢。 - Elham Gdz
@ravneet:我该如何在文本旁设置可绘制对象? - Elham Gdz
3个回答

21

1
我用另一种方式做了,但是图片和文本之间有空隙,我该怎么办?我想减少这个空隙。 - Elham Gdz

14

您可以通过调用TextView.getCompoundDrawables()方法获取引用,然后将右侧drawable的alpha设置为0(不可见)或255(可见)。


0

-> 如果您想要隐藏 'android:drawableEndicon',请设置

注意:电子邮件是视图的findViewById,如edittext

Email.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);

-> 如果你想要显示 'android:drawableEndicon',那么请设置

Email.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接