android按钮背景颜色如何设置
在Android中,可以通过以下方法设置按钮的背景颜色:
- 在XML布局文件中设置按钮的背景颜色:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@color/colorAccent" />
其中,@color/colorAccent
是指向颜色资源文件中定义的颜色。
- 在代码中设置按钮的背景颜色:
Button button = findViewById(R.id.button);
button.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
其中,R.color.colorAccent
是指向颜色资源文件中定义的颜色。
相关问答