Android studio Android Studio:PNG打开按钮
如何在MainActivity中的按钮上设置png图标? 我必须在程序中多次更改此图标,因此无法在xml代码中设置图像 但是在xml中,它位于按钮的中心,因此它是完美的,并且拉伸良好Android studio Android Studio:PNG打开按钮,android-studio,button,png,drawable,Android Studio,Button,Png,Drawable,如何在MainActivity中的按钮上设置png图标? 我必须在程序中多次更改此图标,因此无法在xml代码中设置图像 但是在xml中,它位于按钮的中心,因此它是完美的,并且拉伸良好 android:drawableTop="@drawable/x" 在主要活动中,我不知道该做什么 bt.setCompoundDrawablesWithIntrinsicBounds(null, x, null, null); 使用以下代码: Drawable top = getResources().get
android:drawableTop="@drawable/x"
在主要活动中,我不知道该做什么
bt.setCompoundDrawablesWithIntrinsicBounds(null, x, null, null);
使用以下代码:
Drawable top = getResources().getDrawable(R.drawable.x);
bt.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
要在中心添加图像,可以使用imageButton:
在布局xml中添加下面的图像按钮来代替您的按钮
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton" />
希望这能有所帮助。如果您的目标是API级别17或更高,只需一句简单的话即可实现:
bt.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.x , 0, 0);
此方法将可绘制资源ID作为参数,因此无需使用getDrawable()
自己获取可绘制资源ID。将0
作为参数传递意味着该侧没有图标
祝你好运 有没有办法改变宽度和高度?我怎样才能把这个图像放在按钮的中心呢?我怎样才能把这个图像放在按钮的中心呢?
bt.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.x , 0, 0);