如何在运行时设置按钮的属性"android:drawableTop"

Man*_*esh 61 layout android

如何android:drawableTop在运行时设置按钮的属性" "

Tan*_*dal 130

使用

button.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

将Drawables(如果有)设置为显示在文本的左侧,上方,右侧和下方.如果你不想在那里使用Drawable,请使用0.Drawables的界限将设置为其内在界限.

如果你使用

button.setCompoundDrawables(left, top, right, bottom);

将Drawables(如果有)设置为显示在文本的左侧,上方,右侧和下方.如果你不想在那里使用Drawable,请使用null.Drawables必须已经调用了setBounds(Rect).

  • 谢谢你澄清我使用过.b.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.drawable.x,0,0); (7认同)
  • 是的.使用资源resources = getResources(); Drawable drawable = resources.getDrawable(id); (3认同)

Kir*_*ela 48

Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
Run Code Online (Sandbox Code Playgroud)


小智 22

final Drawable drawableTop = getResources().getDrawable(R.drawable.btn_check_buttonless_on);

btnByCust.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {


 btnByCust.setCompoundDrawablesWithIntrinsicBounds(null, drawableTop , null, null);

        }
    });
Run Code Online (Sandbox Code Playgroud)