Change background of button with border

2019-07-24 01:49发布

问题:

I have created a 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>

and

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

Now I change the color of the background programmatically. The problem is that the border gets removed as soon as I change the background. Is there a way to change to background color of the button and keeping the border?

回答1:

Try this,

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


回答2:

Use following code

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

For Changing color dynamically use following code.

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));
    }


回答3:

From my point of view no need to create cutom drawable just add any layout Apply margin android:layout_margin="1dp" to your button and android:background="#FF4081" for RelativeLayout.Now just change the background of your buttton.

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


回答4:

To apply new color on same drawable use DrawableCompat class.

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


回答5:

Maybe you should try and get a reference to the background drawable and then apply some color, like the code I post below :

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

where #FF0000 is the new color you want to display. That way I think borders will not get removed .