Pat*_*cks 2 android background border button
我创建了一个带边框的按钮:
<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>
Run Code Online (Sandbox Code Playgroud)
和
<Button
android:text="@null"
android:stateListAnimator="@null"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/colorButton"
android:background="@drawable/button_border" />
Run Code Online (Sandbox Code Playgroud)
现在我以编程方式更改背景的颜色。问题是一旦我更改背景,边框就会被删除。有没有办法改变按钮的背景颜色并保持边框?
尝试这个,
Button colorButton = (Button) findViewById(R.id.colorButton);
GradientDrawable background = (GradientDrawable) colorButton.getBackground();
background.setColor(getResources().getColor(R.color.some_color));
Run Code Online (Sandbox Code Playgroud)