android button设置图片
时间: 2024-06-10 17:08:55 浏览: 7
可以使用以下代码设置Button的图片:
```
// 设置Button的背景图片
button.setBackgroundResource(R.drawable.button_bg);
// 设置Button的图标
button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
```
其中,button_bg和icon是图片资源的名称,需要在res目录下的drawable文件夹中添加对应的图片。
相关问题
android button 设置背景图片
你可以使用以下代码来设置 Android 按钮的背景图片:
```java
Button button = findViewById(R.id.button); // 获取按钮对象
button.setBackgroundResource(R.drawable.background_image); // 设置背景图片
```
其中,`R.drawable.background_image` 是你想要设置的背景图片的资源 ID。你需要将该图片放在 `res/drawable` 目录下。
android怎么button放图片
要在 Android Button 中放置图片,可以使用属性 android:drawableLeft、android:drawableRight、android:drawableTop 或 android:drawableBottom。
例如,要在 Button 左侧放置一个图片,可以在 Button 标签中添加以下属性:
```
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button with Image"
android:drawableLeft="@drawable/your_image" />
```
其中,@drawable/your_image 指代了你要放置在 Button 上的图片的资源 ID。
你可以根据需要使用不同的 drawable 属性来放置图片在 Button 上的不同位置。