Android如何以编程方式设置按钮笔划和半径

Android如何以编程方式设置按钮笔划和半径,android,button,styles,Android,Button,Styles,我有48个按钮,用户可以触摸并更改文本和背景颜色 我已经像这样编辑了默认按钮的样式 但是当用户更改背景颜色时,我的结果很糟糕 这些是为dafault按钮设置样式的XML buttons.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable

我有48个
按钮,用户可以触摸并更改文本和背景颜色

我已经像这样编辑了默认
按钮的样式

但是当用户更改背景颜色时,我的结果很糟糕

这些是为dafault
按钮设置样式的XML

buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_pressed"
        android:state_pressed="true" />

    <item android:drawable="@drawable/button_focused"
        android:state_focused="true" />

    <item android:drawable="@drawable/button_default" />

</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="100dp"
        />
    <solid
        android:color="#FFFFFF"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <stroke
        android:width="3dp"
        android:color="#787878"
        />
</shape>
但我失去了半径和笔划属性。 有什么方法可以通过编程来设置它们?
接受其他建议

要实现这一点,您应该通过编程方式设置可绘制图形

Drawable buttonDrawable = context.getResources().getDrawable(R.drawable.buttons.xml);
buttonDrawable.mutate()
changedButton.setBackgroundDrawable(buttonDrawable);

我用这行代码解决了这个问题

changedButton.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);
而不是

changedButton.setBackgroundColor(mat.getColor());
我用
getBackground()
返回默认
按钮的背景,然后用
setColorFilter(int,mode)设置颜色

所以结果变成

//Get all materie inside database
    List<Materia> materia = db.getAllMaterie();
    //change all TextView inputed from user
    if(materia.isEmpty()){
        //do nothing
    }else {
        for (Materia mat : materia) {
            //Change all the Button with values stored inside the database
            int resId = getResources().getIdentifier(mat.getID(), "id", getPackageName());
            final Button changedButton = (Button) findViewById(resId);
            changedButton.setText(mat.getMateria());
            changedButton.setTypeface(null, Typeface.BOLD);
            changedButton.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);

        }
    }
//获取数据库中的所有材料
List material=db.getAllMaterial();
//更改从用户输入的所有文本视图
if(material.isEmpty()){
//无所事事
}否则{
用于(材料垫:材料){
//使用数据库中存储的值更改所有按钮
int resId=getResources().getIdentifier(mat.getID(),“id”,getPackageName());
最终按钮更改按钮=(按钮)findViewById(剩余);
changedButton.setText(mat.getMaterial());
changedButton.setTypeface(null,Typeface.BOLD);
changedButton.getBackground().setColorFilter(mat.getColor(),PorterDuff.Mode.MULTIPLY);
}
}

我必须使用属性stroke和radius创建另一个btn.xml?btnA是我的changedButton?为什么不使用现有的?上面的代码只是示例,但是我已经根据您的变量名进行了更新。我不能使用现有的,因为他们已经设置了颜色!在我的例子中,我采用DB的颜色,但颜色并不总是相同的。现在我试试你的代码!谢谢你的新编辑!后退式拉深(按钮式拉深);不推荐使用!无论如何,它不起作用。
//Get all materie inside database
    List<Materia> materia = db.getAllMaterie();
    //change all TextView inputed from user
    if(materia.isEmpty()){
        //do nothing
    }else {
        for (Materia mat : materia) {
            //Change all the Button with values stored inside the database
            int resId = getResources().getIdentifier(mat.getID(), "id", getPackageName());
            final Button changedButton = (Button) findViewById(resId);
            changedButton.setText(mat.getMateria());
            changedButton.setTypeface(null, Typeface.BOLD);
            changedButton.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);

        }
    }