0

我设法通过 XML 代码创建了一个自定义按钮背景形状

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="24dp" />
            <stroke android:width="2dp" android:color="@color/colorWhite" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="24dp" />
            <stroke android:width="2dp" android:color="@color/colorWhite" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="24dp" />
            <stroke android:width="2dp" android:color="@color/colorWhite" />
            <solid android:color="@color/colorWhite" />
        </shape>
    </item>
</selector>

但是我想知道与之等效的Java代码是什么?请问有什么指南吗?

4

2 回答 2

1

在 Java 中执行此操作要冗长得多,但这里是您需要做的事情。

  • 创建一个new StateListDrawable()
  • 对于每个州:
    • 创建一个new ShapeDrawable(new RoundRectShape(...)). 我不记得构造函数 args 是如何工作的,但您可以进行实验。
    • 用于shapeDrawable.getPaint()获取其 Paint 对象并进行修改。您可能会使用setColor(),setStyle()setStrokeWidth().
    • 构造一个状态集。这是一个由各种 android 状态属性组成的整数数组,例如android.R.attr.state_pressed,用于您想要的状态。
    • 打电话stateListDrawable.addState(stateSet, shapeDrawable)。您可以使用StateSet.NOTHING(或空的 int[])作为默认状态。确保按照它们在 XML 中出现的顺序添加它们。

像这样的东西:

StateListDrawable stateListDrawable = new StateListDrawable();
Shape roundRect = new RoundRectShape(...);
// Add states in order. I'll just demonstrate one.
ShapeDrawable pressed = new ShapeDrawable(roundRect);
Paint paint = pressed.getPaint();
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10f); // this is in pixels, you'll have to convert to dp yourself
int[] pressedState = { android.R.attr.state_pressed };
stateListDrawable.addState(pressedState, pressed);
于 2015-12-12T05:17:18.770 回答
0

您可以像这样处理按钮状态:

StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.drawable.img_pressed));
    states.addState(new int[] { android.R.attr.state_focused }, getResources().getDrawable(R.drawable.img_focused));
    states.addState(new int[] {}, getResources().getDrawable(R.drawable.img_normal));
    button.setBackgroundDrawable(states);

请参阅此示例以供参考:

StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, getSelectedBackground());
    states.addState(new int[] { android.R.attr.state_focused }, getSelectedBackground());
    states.addState(new int[] {}, getNormalBackground());
    button.setBackgroundDrawable(states);

private ShapeDrawable getNormalBackground() {

    int r = 10;
    float[] outerR = new float[] { r, r, r, r, r, r, r, r };
    RoundRectShape rr = new RoundRectShape(outerR, null, null);
    ShapeDrawable drawable = new ShapeDrawable(rr);
    drawable.getPaint().setColor(Color.GRAY);

    return drawable;

}

private ShapeDrawable getSelectedBackground() {

    float[] outerR2 = new float[] { 10, 10, 10, 10, 10, 10, 10, 10 };
    RectF inset2 = new RectF(3, 3, 3, 3);
    float[] innerR2 = new float[] { 9, 9, 0, 0, 0, 5, 0, 0 };
    ShapeDrawable sh2 = new ShapeDrawable(new RoundRectShape(outerR2, inset2, innerR2));
    return sh2;

}

在 JAVA 代码中检查一次定义可绘制形状

于 2015-12-12T05:59:41.677 回答