Android ImageButton 使用方法
时间: 2023-11-17 21:07:27 浏览: 57
1. 在XML布局文件中添加ImageButton组件。
```xml
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
```
2. 在Java代码中获取ImageButton对象。
```java
ImageButton imageButton = findViewById(R.id.imageButton);
```
3. 为ImageButton设置点击事件。
```java
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
}
});
```
4. 设置ImageButton的图片资源。
```java
imageButton.setImageResource(R.drawable.new_image);
```
5. 设置ImageButton的背景色。
```java
imageButton.setBackgroundColor(Color.RED);
```
6. 设置ImageButton的缩放类型。
```java
imageButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
```
7. 设置ImageButton的大小。
```java
imageButton.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)