【问题标题】:Android how to programmatically set Button stroke and radiusAndroid如何以编程方式设置按钮行程和半径
【发布时间】:2016-01-03 16:54:23
【问题描述】:

我有 48 个Button 的活动,用户可以触摸并更改文本和背景颜色。

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

但是当用户更改背景颜色时,我得到了这个糟糕的结果

这些是设置默认 Buttons 样式的 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>

button_default.xml

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

在其他 xml 中仅更改颜色,因此我避免发布它们。 这是以编程方式更改Button的代码。 我从DB 中取出所有已更改的Button,并使用ID 保存并设置Button 的颜色。

   //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.setBackgroundColor(mat.getColor());

        }
    }

但是我失去了半径和描边属性。 有一些方法可以以编程方式设置它们吗? 接受其他建议!

【问题讨论】:

    标签: android button styles


    【解决方案1】:

    要实现这一点,您应该以编程方式设置可绘制对象。

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

    【讨论】:

    • 我必须用属性 stroke 和 radius 创建另一个 btn.xml 吗? btnA 是我的 changedButton?
    • 为什么不使用现有的?
    • 以上代码只是示例,但我已根据您的变量名称对其进行了更新。
    • 我不能使用现有的,因为他们已经设置了颜色!就我而言,我从 DB 中获取颜色,但并不总是相同的。现在我试试你的代码!感谢您的新编辑!
    • setBackgroundDrawable(buttonDrawable);已弃用!无论如何它不起作用。
    【解决方案2】:

    我用这行代码解决了问题

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

    代替

    changedButton.setBackgroundColor(mat.getColor());
    

    我用getBackground()取回默认Button的背景,然后用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);
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-01-19
      • 1970-01-01
      • 2019-04-13
      • 2016-01-07
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多