Android 在代码中定义选择器

Android 在代码中定义选择器,android,Android,我有以下选择器: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#00000" /> <corners android:radius="10dp" /&g

我有以下选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#00000" />
            <corners android:radius="10dp" />
            <stroke android:width="5px" android:color="#FFFFFF" />
        </shape>
    </item>

</selector>
这给了我一个带有白色边缘的按钮,但我无法设置按钮的背景色。 这在代码中是如何实现的?

试试这段代码

StateListDrawable states = new StateListDrawable();

    float[] roundedCorner = new float[] { 10, 10, 10, 10, 10, 10, 10, 10 };
    float[] innerRoundedCorner = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    RectF inset = new RectF(0, 0, 0, 0);

    RoundRectShape round = new RoundRectShape(roundedCorner, inset, innerRoundedCorner);        
    LinearGradient lg = new LinearGradient(0, 0, 100, 100, Color.BLUE, Color.CYAN, TileMode.REPEAT);

    ShapeDrawable shape = new ShapeDrawable(round);
    shape.getPaint().setStyle(Style.FILL_AND_STROKE);
    //shape.getPaint().setColor(Color.WHITE);
    shape.getPaint().setShader(lg);

    states.addState(new int[] {}, shape);

    button.setBackgroundDrawable(shape);
定义添加到RoundRectShape中的RectF的inset对象时,传递0值,即得到完整的RoundRectShape


选中post

RoundRectShape可以在RectF上接受null,那么您将拥有一个填充的形状。 此外,当您使用状态时,您可以在选择器中指定属性:state_pressed、state_single等

请尝试以下代码:

private static StateListDrawable getStateListDrawable(int color, Context context) {
    StateListDrawable stateListDrawable = new StateListDrawable();

    float[] roundedCorner = new float[]{4, 4, 4, 4, 4, 4, 4, 4};
    float[] innerRoundedCorner = new float[]{0, 0, 0, 0, 0, 0, 0, 0};
    RoundRectShape roundedShape = new RoundRectShape(roundedCorner, null, innerRoundedCorner);

    ShapeDrawable pressedDrawable = new ShapeDrawable(roundedShape);
    pressedDrawable.getPaint().setColor(context.getResources().getColor(R.color.default_bg));
    stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);

    ShapeDrawable normalDrawable = new ShapeDrawable(roundedShape);
    normalDrawable.getPaint().setColor(context.getResources().getColor(color));
    stateListDrawable.addState(new int[]{}, normalDrawable);

    return stateListDrawable;
} 

为什么不使用Android.R.attr.state_XXX中的标准Android状态代码,并且将
形状
对象而不是
状态
对象设置为BackgroundDrawable是否正确?@teoREtik Android.R.attr.state只定义要为哪个状态设置Drawable,我想能够改变颜色的代码根据一些条件。所以我需要一种方法来绘制代码。@teoREtik代码是正确的,并且可以工作。setBackgroundDrawable将同时接受形状和状态参数。将参数更改为状态以获得完整性。以下代码不会在形状周围生成边。我希望边缘颜色与内部颜色不同。
private static StateListDrawable getStateListDrawable(int color, Context context) {
    StateListDrawable stateListDrawable = new StateListDrawable();

    float[] roundedCorner = new float[]{4, 4, 4, 4, 4, 4, 4, 4};
    float[] innerRoundedCorner = new float[]{0, 0, 0, 0, 0, 0, 0, 0};
    RoundRectShape roundedShape = new RoundRectShape(roundedCorner, null, innerRoundedCorner);

    ShapeDrawable pressedDrawable = new ShapeDrawable(roundedShape);
    pressedDrawable.getPaint().setColor(context.getResources().getColor(R.color.default_bg));
    stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);

    ShapeDrawable normalDrawable = new ShapeDrawable(roundedShape);
    normalDrawable.getPaint().setColor(context.getResources().getColor(color));
    stateListDrawable.addState(new int[]{}, normalDrawable);

    return stateListDrawable;
}