如果要在Button上面添加一个图片,可以在xml中添加属性:
[html] view plaincopy
- <Button
- android:id="@+id/AppBtn"
- android:layout_centerVertical="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:drawableLeft="@drawable/defaultrewardicon"
- android:text="" />
[html] view plaincopy
- android:drawableLeft="@drawable/defaultrewardicon"
如果要在代码中动态修改这个图片,用下面的方法:
[java] view plaincopy
- Drawable rewardIcon=getResources().getDrawable(m_RewardIcon);
- if(rewardIcon!=null)
- {
- rewardIcon.setBounds(0, 0, rewardIcon.getMinimumWidth(), rewardIcon.getMinimumHeight());
- appBtn.setCompoundDrawables(rewardIcon, null, null, null);
- }
时间: 2024-10-13 14:41:58