要设置Android按钮的颜色,可以使用以下方法:
- 在XML布局文件中设置按钮的背景颜色:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@color/colorPrimary" />
- 在java代码中设置按钮的背景颜色:
Button button = findViewById(R.id.button);
button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
- 使用Selector设置按钮的不同状态下的颜色:
在res/drawable文件夹下创建一个selector文件,例如button_selector.xml,内容如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorAccent" />
<item android: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按钮的颜色。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/903916.html