更改带边框的按钮背景

Change background of button with border

我创建了一个带边框的按钮:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

    <solid android:color="#FFFFFFFF" />

    <stroke
        android:width="1dp"
        android:color="#FFCCCCCC" />
</shape>

<Button
        android:text="@null"
        android:stateListAnimator="@null"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/colorButton"
        android:background="@drawable/button_border" />

现在我以编程方式更改背景颜色。问题是一旦我更改背景,边框就会被删除。有没有办法改变按钮的背景颜色并保持边框?

使用以下代码

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

    <solid android:color="#FFFFFFFF" />

    <stroke
        android:width="1dp"
        android:color="#FFCCCCCC" />
 <gradient
        android:startColor="@color/white"
        android:centerColor="@color/white"
        android:endColor="@color/white"/>

</shape>

要动态更改颜色,请使用以下代码。

Drawable background = yourView.getBackground();
    if (background instanceof ShapeDrawable) {
        // cast to 'ShapeDrawable'
        ShapeDrawable shapeDrawable = (ShapeDrawable) background;
        shapeDrawable.getPaint().setColor(getResources().getColor(R.color.colorToSet));
    } else if (background instanceof GradientDrawable) {
        // cast to 'GradientDrawable'
        GradientDrawable gradientDrawable = (GradientDrawable) background;
        gradientDrawable.setColor(getResources().getColor(R.color.colorToSet));
    } else if (background instanceof ColorDrawable) {
        // alpha value may need to be set again after this call
        ColorDrawable colorDrawable = (ColorDrawable) background;
        colorDrawable.setColor(getResources().getColor(R.color.colorToSet));
    }

也许您应该尝试获取对背景可绘制对象的引用,然后应用一些颜色,例如下面我 post 的代码:

    GradientDrawable gradientDrawable = (GradientDrawable) colorButton.getBackground();
    gradientDrawable.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC);

其中 #FF0000 是您要显示的新颜色。 这样我认为边界不会被删除。

要在同一可绘制对象上应用新颜色,请使用 DrawableCompat class。

DrawableCompat.setTintList(d,drawableTintColor); // d is drawable object and drawableTintColor is color you want to apply

试试这个,

    Button colorButton = (Button) findViewById(R.id.colorButton);
    GradientDrawable background = (GradientDrawable) colorButton.getBackground();
    background.setColor(getResources().getColor(R.color.some_color));

从我的角度来看,无需创建自定义可绘制对象只需添加任何布局将边距 android:layout_margin="1dp" 应用到您的按钮并为 RelativeLayout.Now 应用边距 android:background="#FF4081" 只需更改 按钮的背景

 <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF4081"> 
            <Button
                android:id="@+id/colorButton"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="1dp"
                android:background="@android:color/white"
                android:text="@null" />
        </RelativeLayout>