为什么自定义控件无法在设计器中显示属性更改

时间:2019-07-05 20:12:34

标签: java android

我创建了自定义TextView,它根据属性“ state”更改了背景色。控件在应用程序中完美运行,但在设计器中不会改变颜色。

private void prepare(Context context) {
    final TypedArray array = context.obtainStyledAttributes(R.styleable.StatusView);
    setState(State.values()[array.getIndex(R.styleable.StatusView_state)]);
    array.recycle();
}

public State getState() {
    return state;
}

public void setState(State state) {
    this.state = state;
    if (state == State.NO_STATE) {
        setBackgroundResource(R.color.noStateBackground);
    } else if (state == State.EMPTY) {
        setBackgroundResource(R.color.emptyBackground);
    } else if (state == State.ERROR) {
        setBackgroundResource(R.color.errorBackground);
    } else {
        setBackgroundResource(R.color.okBackground);
    }
}

XML声明的属性

<declare-styleable name="StatusView">
    <attr name="state" format="enum">
        <enum name="no_style" value="0"/>
        <enum name="empty" value="1"/>
        <enum name="error" value="2"/>
        <enum name="ok" value="3"/>
    </attr>
</declare-styleable>

我已经尝试重建缓存并使之失效,没有任何帮助

0 个答案:

没有答案