更改带有边框的按钮的背景

更改带有边框的按钮的背景

本文介绍了更改带有边框的按钮的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

推荐答案

尝试一下,

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

这篇关于更改带有边框的按钮的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:36