要设置Android按钮的颜色,可以使用以下方法: 在XML布局文件中设置按钮的背景颜色: <Button android:id="@+id/button"
要设置Android按钮的颜色,可以使用以下方法:
- 在XML布局文件中设置按钮的背景颜色:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@color/colorPrimary"/>
Buttonbutton=findViewById(R.id.button);
button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:state_pressed="true"android:drawable="@color/colorAccent"/>
<itemandroid:drawable="@color/colorPrimary"/>
</selector>
然后在布局文件或java代码中设置按钮的背景为这个selector:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/button_selector"/>
通过以上方法可以设置Android按钮的颜色。
版权声明
本文仅代表作者观点,不代表博信信息网立场。