1

我在可绘制文件夹中有用于圆形按钮的 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>

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

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

4

1 回答 1

6

这应该有效:

Button button = (Button) findViewById(R.id.myButton);
button.setBackgroundResource(R.drawable.rounded);
于 2013-10-04T21:45:05.150 回答