在Android中,你可以通过设置Button的背景来加入图片。以下是加入图片的几种方式:
在XML布局文件中设置Button的背景为图片资源:<Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/image" />其中,@drawable/image表示图片资源的引用,你需要将图片文件放置在res/drawable目录下。
Button button = findViewById(R.id.button);button.setBackgroundResource(R.drawable.image);使用android:drawableLeft、android:drawableRight、android:drawableTop或android:drawableBottom属性将图片与文字一起显示在Button上:<Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button"android:drawableLeft="@drawable/image" />你可以将@drawable/image替换为你的图片资源。
Button button = findViewById(R.id.button);Drawable image = getResources().getDrawable(R.drawable.image);button.setCompoundDrawablesWithIntrinsicBounds(image, null, null, null);这里的R.drawable.image表示图片资源的引用,你需要将图片文件放置在res/drawable目录下。
以上是几种常见的添加图片到Button的方法,你可以根据具体需求选择适合的方式。

