单选按钮drawable top没有显示setCompoundDrawablesWithIntrinsicBounds();并使用setButtonDrawable();显示在错误的位置

时间:2017-10-27 22:13:34

标签: android

我在设置中将按钮设置为可绘制的底部。在java中,我使用setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.wrong,0,R.drawable.check);方法设置了drawable top,并且没有显示drawable top。

所以我尝试了setButtonDrawable();的不同方法,它不会改变单选按钮drawablebottom,这很好,它将Drawable设置在Button旁边。

image

如何将其位置改为顶部?

1 个答案:

答案 0 :(得分:0)

您可以重复使用之前的drawable来保留drawable底部:

Drawable drawableTop = ContextCompat.getDrawable(getContext(),R.drawable.wrong);
//Drawable drawableBottom = ContextCompat.getDrawable(getContext(),R.drawable.check);

Drawable[] drawables = button.getCompoundDrawables();
// left top right bottom
button.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawableTop, drawables[2], drawables[3]);

或者您可以尝试以下方法:

Drawable drawableTop = ContextCompat.getDrawable(getContext(),R.drawable.wrong);
Drawable drawableBottom = ContextCompat.getDrawable(getContext(),R.drawable.check);

int topHeight = drawableTop.getIntrinsicHeight(); 
int topWidth = drawableTop.getIntrinsicWidth();   
drawableTop.setBounds(0, 0, topWidth, topHeight);

topHeight = drawableBottom.getIntrinsicHeight(); 
topWidth = drawableBottom.getIntrinsicWidth();   
drawableBottom.setBounds(0, 0, topWidth, topHeight);

button.setCompoundDrawables(null, drawableTop, null, drawableBottom);