新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

Android button如何加图片,android button text

发布时间:2023-09-27 19:40:24

Android button如何加图片

在Android中,你可以通过设置Button的背景来加入图片。以下是加入图片的几种方式:

  1. 在XML布局文件中设置Button的背景为图片资源:

其中,@drawable/image表示图片资源的援用,你需要将图片文件放置在res/drawable目录下。

  1. 在Java代码中设置Button的背景为图片资源:
Button button = findViewById(R.id.button);
button.setBackgroundResource(R.drawable.image);
  1. 使用android:drawableLeftandroid:drawableRightandroid:drawableTopandroid:drawableBottom属性将图片与文字一起显示在Button上:

你可以将@drawable/image替换为你的图片资源。

  1. 通过代码设置Button的图片:
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的方法,你可以根据具体需求选择合适的方式。