无法让 GridLayout 和 GridLayoutParser 与 proteus 一起工作
Cannot get GridLayout and GridLayoutParser to work with proteus
我正在尝试使用 Proteus 在 FrameLayout
中给 GridLayout
充气。
我尝试按照此处的建议实施 GridLayoutParser
和 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 一样出现在一行中。
解决了。 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());
}
});
}
为了处理 TextView
的 layout_columnWeight
属性 我实现了以下 class 覆盖已经提供的 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")));
正确处理属性后就获得了所需的视图。
我正在尝试使用 Proteus 在 FrameLayout
中给 GridLayout
充气。
我尝试按照此处的建议实施 GridLayoutParser
和 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 一样出现在一行中。
解决了。 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());
}
});
}
为了处理 TextView
的 layout_columnWeight
属性 我实现了以下 class 覆盖已经提供的 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")));
正确处理属性后就获得了所需的视图。