1

我正在尝试使用 Proteus充气GridLayoutFrameLayout

我尝试按照这里的建议实施GridLayoutParserand - Proteus 支持 GridLayout 吗?如果不是,那么有什么替代方案?.GridLayout

这是我尝试过的-
查看-

public class ProteusGridLayout extends GridLayout implements ProteusView {
    private ProteusViewManager viewManager;

    public ProteusGridLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ProteusGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProteusGridLayout(Context context) {
        super(context);
    }

    public ProteusGridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public ProteusViewManager getViewManager() {
        return viewManager;
    }

    @Override
    public void setViewManager(ProteusViewManager proteusViewManager) {
        viewManager = proteusViewManager;
    }
}

解析器 -

public class ProteusGridLayoutParser extends WrappableParser<ProteusGridLayout> {

    public ProteusGridLayoutParser(Parser<ProteusGridLayout> wrappedParser) {
        super(wrappedParser);
    }

    @Override
    public ProteusView createView(ViewGroup parent, JsonObject layout, JsonObject data, Styles styles, int index) {
        return new ProteusGridLayout(parent.getContext());
    }
}

我试图渲染的 Proteus json 数据 -

{
    "type": "GridLayout",
    "layout_width": "match_parent",
    "layout_height": "match_parent",
    "layout_gravity": "fill_horizontal",
    "columnCount": "2",
    "useDefaultMargins": "true",
    "children": [{
            "type": "TextView",
            "layout_width": "wrap_content",
            "layout_height": "wrap_content",
            "layout_columnWeight": "1",
            "layout_marginTop": "8dp",
            "layout_marginLeft": "16dp",
            "textSize": "20dp",
            "textColor": "@android:color/background_dark",
            "text": "244536"
        },
        {
            "type": "TextView",
            "layout_width": "wrap_content",
            "layout_height": "wrap_content",
            "layout_columnWeight": "1",
            "layout_marginTop": "8dp",
            "layout_marginLeft": "16dp",
            "textSize": "20dp",
            "textColor": "@android:color/background_dark",
            "text": "244536"
        },
        {
            "type": "TextView",
            "layout_width": "wrap_content",
            "layout_height": "wrap_content",
            "layout_columnWeight": "1",
            "layout_marginTop": "8dp",
            "layout_marginLeft": "16dp",
            "textSize": "20dp",
            "textColor": "@android:color/background_dark",
            "text": "244536"
        }
    ]
}

我在 logcat - 中得到了这个D/android.widget.GridLayout: horizontal constraints: x3-x0>=660, x3-x2<=164, x2-x1<=164, x1-x0<=164 are inconsistent; permanently removing: x3-x2<=164.。GridLayout 未正确呈现,三个元素像 LinearLayout 一样出现在一行中。

4

1 回答 1

1

解决了。缺少以下ProteusGridLayoutParser处理GridLayout特定属性的方法 -

@Override
    protected void prepareHandlers() {
        super.prepareHandlers();
        this.addHandler(new Attributes.Attribute("columnCount"), new AttributeProcessor<ProteusGridLayout>() {
            @Override
            public void handle(String s, JsonElement jsonElement, ProteusGridLayout proteusGridLayout) {
                proteusGridLayout.setColumnCount(jsonElement.getAsInt());
            }
        });
        this.addHandler(new Attributes.Attribute("useDefaultMargins"), new AttributeProcessor<ProteusGridLayout>() {
            @Override
            public void handle(String s, JsonElement jsonElement, ProteusGridLayout proteusGridLayout) {
                proteusGridLayout.setUseDefaultMargins(jsonElement.getAsBoolean());
            }
        });
    }

为了处理我的layout_columnWeight属性,TextView我实现了以下类,覆盖了已经提供的TextViewParser-

public class CustomProteusTextViewParser extends TextViewParser<ProteusTextView> {
    public CustomProteusTextViewParser(Parser<ProteusTextView> wrappedParser) {
        super(wrappedParser);
    }

    @Override
    public ProteusView createView(ViewGroup parent, JsonObject layout, JsonObject data, Styles styles, int index) {
        return new ProteusTextView(parent.getContext());
    }

    @Override
    protected void prepareHandlers() {
        super.prepareHandlers();

        this.addHandler(new Attributes.Attribute("layout_columnWeight"), new AttributeProcessor<ProteusTextView>() {
            @Override
            public void handle(String s, JsonElement jsonElement, ProteusTextView proteusTextView) {
                GridLayout.LayoutParams layoutParams = (GridLayout.LayoutParams) proteusTextView.getLayoutParams();
                layoutParams.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, jsonElement.getAsFloat());
                proteusTextView.setLayoutParams(layoutParams);
            }
        });
    }
}

最后将此解析器注册为处理程序TextView-

LayoutBuilder layoutBuilder = new LayoutBuilderFactory().getDataParsingLayoutBuilder();
layoutBuilder.registerHandler("TextView", new CustomProteusTextViewParser((Parser) layoutBuilder.getHandler("View")));

一旦正确处理了属性,就获得了所需的视图。

于 2019-07-16T08:39:13.547 回答