2013-10-04 38 views
1

我有可繪製的文件夾中的xml文件的圓角按鈕 - rounded.xml。如何在java中更改按鈕背景?

<?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?

回答

5

這應該工作:

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