gpt4 book ai didi

java - 如何在java中更改按钮背景?

转载 作者:行者123 更新时间:2023-11-29 07:06:25 26 4
gpt4 key购买 nike

我在圆形按钮的可绘制文件夹中有 xml 文件 - rounded.xml。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ccc" />
<corners android:radius="5dp" />
<stroke android:width="1dp" android:color="#000" />
</shape>

通常我通过在按钮 xml 中使用 android:background = "@drawable/rounded" 来设置它,但我想创建一些具有不同颜色的 xml 文件,以便我可以更改应用程序主题。

你知道我应该如何为按钮设置背景,但在 java 中?

最佳答案

这应该有效:

Button button = (Button) findViewById(R.id.myButton);
button.setBackgroundResource(R.drawable.rounded);

关于java - 如何在java中更改按钮背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com