xml代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners android:radius="5dp" />
<stroke
android:width="1dp"
android:color="@color/questionBankLightPurple" />
</shape>
Java代码:
获取得drawable,发现其实返回的是一个 android.graphics.drawable.GradientDrawable;
/**
* 产生shape类型的drawable
*
* @param solidColor
* @param strokeColor
* @param strokeWidth
* @param radius
* @return
*/
public static GradientDrawable getDrawable(int solidColor, int strokeColor, int strokeWidth, float radius) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(solidColor);
drawable.setStroke(strokeWidth, strokeColor);
drawable.setCornerRadius(radius);
return drawable;
}