簡體   English   中英

Android如何使用按鈕更改圖標

[英]Android How to change icon with button

這是我的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("???");

采用

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

以編程方式更改android:drawableRight屬性。 這里的文件

嘗試使用setCompoundDrawablesWithIntrinsicBounds在運行時設置各自的可繪制對象:

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

在您的情況下:

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

由於您使用的是drawableRight,因此您可能需要使用它

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

嘗試這個

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

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

用這個

w1.setBackgroundResource(R.drawable.icon_next);

嘗試這個 :

w1.setBackgroundResource(R.drawable.icon_next);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM