Android如何使用按钮更改图标

时间:2015-06-10 10:18:51

标签: android android-layout button view

这是我的XML

<Button
  android:id="@+id/w1"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:background="@color/buttons_background"
  android:clickable="true"
  android:drawableRight="@drawable/icon_more"
  android:fontFamily="roboto medium"
  android:paddingLeft="30dp"
  android:paddingRight="30dp"
  android:text="@string/w1_btn_more"
  android:textColor="@color/text_color"
  android:textSize="17sp" />

如何从icon_more更改为icon_next。

一段代码Java

Button w1 = (Button) view.findViewById(R.id.w1);
w1.set("???");

7 个答案:

答案 0 :(得分:3)

尝试使用setCompoundDrawablesWithIntrinsicBounds在运行时设置相应的drawable:

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

在你的情况下:

button.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.icon_next,0);

答案 1 :(得分:2)

使用

w1.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.next, 0);

以编程方式更改android:drawableRight属性。 Here文档

答案 2 :(得分:2)

Drawable img = getContext().getResources().getDrawable( R.drawable.your_drwable);
img.setBounds( 0, 0, 60, 60 );
imgView.setCompoundDrawables( null, null, img, null );

答案 3 :(得分:2)

试试这个

Drawable img = getContext().getResources().getDrawable( R.drawable.icon_next);

img.setBounds(60,60,0,0); txtVw.setCompoundDrawables(img,null,null,null);

答案 4 :(得分:1)

使用此

w1.setBackgroundResource(R.drawable.icon_next);

答案 5 :(得分:1)

由于您使用的是drawableRight,因此您可能希望使用此

w1.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_next, 0);

答案 6 :(得分:1)

试试这个:

w1.setBackgroundResource(R.drawable.icon_next);