Android button如何加图片
在Android中,你可以通过设置Button的背景来加入图片。以下是加入图片的几种方式:
其中,@drawable/image
表示图片资源的援用,你需要将图片文件放置在res/drawable
目录下。
Button button = findViewById(R.id.button);
button.setBackgroundResource(R.drawable.image);
android:drawableLeft
、android:drawableRight
、android:drawableTop
或android:drawableBottom
属性将图片与文字一起显示在Button上:
你可以将@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的方法,你可以根据具体需求选择合适的方式。
TOP