android 自定义button
时间: 2023-06-30 10:21:39 浏览: 124
要自定义 Android Button,您可以按照以下步骤操作:
1. 创建一个新的类来扩展 Button 类。
2. 在新类中编写自定义 Button 的代码,例如更改背景,更改字体样式等。
3. 在 XML 布局文件中使用您的自定义 Button 类。
以下是一个简单的自定义 Button 类的示例代码:
```java
public class CustomButton extends Button {
public CustomButton(Context context) {
super(context);
init();
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// 在这里添加您的自定义代码,例如更改背景,更改字体样式等。
}
}
```
在 XML 布局中使用自定义 Button:
```xml
<com.example.CustomButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Custom Button" />
```
您可以根据自己的需求添加其他自定义属性和方法。
阅读全文
相关推荐













