2015-06-10 122 views
0

這是我的XML的Android如何更改圖標,按鈕

<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("???"); 

回答

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

使用

件這

w1.setBackgroundResource(R.drawable.icon_next); 
2

使用

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

以編程方式更改android:drawableRight屬性。 Here文檔

1

由於您使用的是drawableRight,你可能想用這個

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

嘗試使用setCompoundDrawablesWithIntrinsicBounds在運行時設置相應的繪製:

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

在你的情況下:

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

嘗試這種

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

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

1

試試這個:

w1.setBackgroundResource(R.drawable.icon_next);