android按钮背景颜色如何设置

1208
2024/4/26 10:22:09
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Android中,可以通过以下方法设置按钮的背景颜色:

  1. 在XML布局文件中设置按钮的背景颜色:
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:background="@color/colorAccent" />

其中,@color/colorAccent 是指向颜色资源文件中定义的颜色。

  1. 在代码中设置按钮的背景颜色:
Button button = findViewById(R.id.button);
button.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));

其中,R.color.colorAccent 是指向颜色资源文件中定义的颜色。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: android悬浮窗贴边的方法是什么