我希望能够以编程方式创建选择器图形。该形状必须符合以下形式:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid android:color="#4aa5d4" />
</shape></item>
<item><shape>
<stroke android:width="1dp" android:color="#4aa5d4" />
</shape></item>
</selector>
为什么?因为我想要这两种颜色可以改变。我知道我需要创建某种
Drawable
来实现这一点。我已经成功地创建了自己的GradientDrawables
,就像这样:public GradientDrawable getBackgroundGradient() {
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] { BACKGROUND_GRADIENT_TOP_COLOR, BACKGROUND_GRADIENT_BOTTOM_COLOR });
return gd;
}
但现在我需要一个SelectorDrawable。