layoutconfig support margin

This commit is contained in:
pengfei.zhou
2019-07-24 19:58:30 +08:00
parent cfde85b773
commit d180c6df22
10 changed files with 85 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ import android.util.SparseArray;
import android.view.ViewGroup;
import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.utils.DoricUtils;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;
@@ -79,5 +80,26 @@ public abstract class GroupNode<F extends ViewGroup> extends ViewNode<F> {
return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
public abstract void blendChild(ViewNode viewNode, JSObject jsObject);
protected void blendChild(ViewNode viewNode, JSObject jsObject) {
JSValue jsValue = jsObject.getProperty("margin");
ViewGroup.LayoutParams layoutParams = viewNode.getLayoutParams();
if (jsValue.isObject() && layoutParams instanceof ViewGroup.MarginLayoutParams) {
JSValue topVal = jsValue.asObject().getProperty("top");
if (topVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(topVal.asNumber().toFloat());
}
JSValue leftVal = jsValue.asObject().getProperty("left");
if (leftVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = DoricUtils.dp2px(leftVal.asNumber().toFloat());
}
JSValue rightVal = jsValue.asObject().getProperty("right");
if (rightVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).rightMargin = DoricUtils.dp2px(rightVal.asNumber().toFloat());
}
JSValue bottomVal = jsValue.asObject().getProperty("bottom");
if (bottomVal.isNumber()) {
((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin = DoricUtils.dp2px(bottomVal.asNumber().toFloat());
}
}
}
}

View File

@@ -18,7 +18,7 @@ public class HLayoutNode extends LinearNode {
}
@Override
public LinearLayout build(JSObject jsObject) {
protected LinearLayout build(JSObject jsObject) {
LinearLayout linearLayout = super.build(jsObject);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
return linearLayout;

View File

@@ -18,7 +18,7 @@ public class ImageNode extends ViewNode<ImageView> {
}
@Override
public ImageView build(JSObject jsObject) {
protected ImageView build(JSObject jsObject) {
return null;
}
}

View File

@@ -20,11 +20,16 @@ public class LinearNode extends GroupNode<LinearLayout> {
}
@Override
public void blendChild(ViewNode viewNode, JSObject layoutConfig) {
protected void blendChild(ViewNode viewNode, JSObject layoutConfig) {
super.blendChild(viewNode, layoutConfig);
JSValue jsValue = layoutConfig.getProperty("alignment");
if (jsValue.isNumber()) {
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
}
JSValue weight = layoutConfig.getProperty("weight");
if (weight.isNumber()) {
((LinearLayout.LayoutParams) viewNode.getLayoutParams()).weight = weight.asNumber().toInt();
}
}
@Override
@@ -33,7 +38,7 @@ public class LinearNode extends GroupNode<LinearLayout> {
}
@Override
public LinearLayout build(JSObject jsObject) {
protected LinearLayout build(JSObject jsObject) {
return new LinearLayout(getContext());
}

View File

@@ -20,7 +20,8 @@ public class StackNode extends GroupNode<FrameLayout> {
}
@Override
public void blendChild(ViewNode viewNode, JSObject jsObject) {
protected void blendChild(ViewNode viewNode, JSObject jsObject) {
super.blendChild(viewNode, jsObject);
JSValue jsValue = jsObject.getProperty("alignment");
if (jsValue.isNumber()) {
((FrameLayout.LayoutParams) viewNode.getLayoutParams()).gravity = jsValue.asNumber().toInt();
@@ -28,7 +29,7 @@ public class StackNode extends GroupNode<FrameLayout> {
}
@Override
public FrameLayout build(JSObject jsObject) {
protected FrameLayout build(JSObject jsObject) {
return new FrameLayout(getContext());
}

View File

@@ -21,7 +21,7 @@ public class TextNode extends ViewNode<TextView> {
}
@Override
public TextView build(JSObject jsObject) {
protected TextView build(JSObject jsObject) {
return new TextView(getContext());
}

View File

@@ -18,7 +18,7 @@ public class VLayoutNode extends LinearNode {
}
@Override
public LinearLayout build(JSObject jsObject) {
protected LinearLayout build(JSObject jsObject) {
LinearLayout linearLayout = super.build(jsObject);
linearLayout.setOrientation(LinearLayout.VERTICAL);
return linearLayout;

View File

@@ -41,7 +41,7 @@ public abstract class ViewNode<T extends View> extends DoricComponent {
return getDoricContext().getContext();
}
public abstract T build(JSObject jsObject);
protected abstract T build(JSObject jsObject);
void blend(JSObject jsObject, ViewGroup.LayoutParams layoutParams) {
mLayoutParams = layoutParams;