Android 单击“在中设置复选标记”按钮

Android 单击“在中设置复选标记”按钮,android,button,Android,Button,我有一个按钮,当点击它时,会在按钮的右侧显示一个复选标记图像。 显示此复选标记的最佳方式是什么? 我本来希望隐藏图像,然后单击“显示”,但我无法将其显示在适当的位置。我在文档中也看到了android:drawableRight,但是有没有办法在单击之前隐藏它 按钮和复选标记图像的xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:gr

我有一个按钮,当点击它时,会在按钮的右侧显示一个复选标记图像。 显示此复选标记的最佳方式是什么? 我本来希望隐藏图像,然后单击“显示”,但我无法将其显示在适当的位置。我在文档中也看到了android:drawableRight,但是有没有办法在单击之前隐藏它

按钮和复选标记图像的xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:onClick="myClickHandler"
    android:id="@+id/btn"/>
<ImageView
    android:layout_height="24px"
    android:layout_width="24px"
    android:id="@+id/check"
    android:src="@drawable/check_mark"
    android:visibility="gone"
    />

谢谢

您需要这样做:

Button btn = (Button)getViewById(R.id.btn);
Drawable drawable = getResources().getDrawable(R.drawable.chec_mark);

//hide drawable with this call
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null); //order of params (left, top, right, bottom)

//show drawable on right side of button with this call (in your onclick method)
btn.setCompoundDrawablesWithIntrinsicBounds(null,null,drawable,null);

因此,您无法正确对齐它,无法通过单击按钮隐藏/显示它?是的,我可以隐藏/取消隐藏它,只是无法对齐它。我试着放入一个相对布局,但当我这样做时,它将按钮移到了屏幕的左侧,所以我尝试了帧布局,但无法将其放到正确的位置/不知道如何将其放到正确的位置。如果有帮助,按钮的宽度是设定的(屏幕大小的5/6)。