[英]Programmatically remove the left side image of button but not on right side?
您好,我有两个可左右绘制的图像按钮,我只想删除左侧图像,并希望保留右侧图像我的按钮xml代码在下面。
<Button
android:id="@+id/fragment_add_to_cart_btn_proceed_to_checkout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorRed"
android:drawableEnd="@drawable/ic_action_next"
android:drawablePadding="5dp"
android:drawableStart="@drawable/ic_rounded_cart"
android:gravity="left|center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/proceed_to_checkout"
android:textColor="@color/colorWhite" />
我已经像下面那样应用了一些代码,但是它不起作用
button.setCompoundDrawables(null, null, null, null);
如果您有任何解决方案,请回复? 谢谢。
尝试
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, <your_left_drawable>, 0);
希望它能对您有所帮助。
您应该使用button.setCompoundDrawables(null, null, R.drawable.ic_action_next, null);
尝试:
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, <The right drawable>, 0);
订购:
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_rounded_cart, 0); // if you want to remove the left drawable
在
onCreate
这样做。 给您需要显示的image
资源,否则将给0
。
int imgResource = R.drawable.common_google_signin_btn_icon_dark;
fragment_add_to_cart_btn_proceed_to_checkout = (Button) findViewById(R.id.fragment_add_to_cart_btn_proceed_to_checkout);
fragment_add_to_cart_btn_proceed_to_checkout.setCompoundDrawablesWithIntrinsicBounds(0, 0, imgResource, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.