0

是否可以在其 Activity 中的 android 按钮中指定边框?我想保留按钮png背景并为其添加指定边框并将其删除或动态更改其颜色。
一个拼图板由几个按钮组成,以便被选中,如果选择不正确则取红色边框。

4

1 回答 1

1

使用ImageButton:使用您的 png 图像作为android:src背景边框,并添加一个可绘制的 xml。

<ImageButton
    android:id="@+id/button"
    android:layout_width="75dp"
    android:layout_height="70dp"
    android:background="@drawable/border"
    android:padding="15dp"
    android:scaleType="fitCenter"
    android:src="@drawable/your_image" />

res/drawable/border.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="3dp"
        android:color="@color/stroke_color"></stroke>
    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />
</shape>

要以编程方式更改形状边框颜色,请使用:

ImageButton button = (ImageButton) findViewById(R.id.button);
GradientDrawable backgroundGradient = (GradientDrawable) button.getBackground();
backgroundGradient.setStroke(5, Color.RED);  // set stroke width and stroke color
于 2017-05-14T19:03:07.893 回答