button 加attr
时间: 2023-07-28 07:26:18 浏览: 34
好的,你想要了解如何在 Android 中使用 `Button` 控件添加属性吗?
首先,我们可以在布局文件中使用 `android:attr/` 前缀来添加属性,例如:
```xml
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button"
android:attr/my_custom_attribute="my_custom_value" />
```
这里,我们使用 `android:attr/` 前缀来添加自定义属性 `my_custom_attribute` 并给它赋值为 `my_custom_value`。
然后,在 Java 代码中,我们可以使用 `Button` 对象的 `getAttributeValue()` 方法来获取该属性的值,例如:
```java
Button myButton = findViewById(R.id.my_button);
String myCustomValue = myButton.getAttributeValue(null, "my_custom_attribute");
```
这里,我们使用 `getAttributeValue()` 方法来获取自定义属性 `my_custom_attribute` 的值,并将其存储在字符串变量 `myCustomValue` 中。
希望这可以帮助到你,如果还有其他问题,请随时提出。