📐 ConstraintLayout
Tworzenie ConstraintLayout
let constraint = createView("constraint", {
width: "match",
height: "match"
});
Constraints
constrainToParent(viewId, "top", 16);
constrainToParent(viewId, "bottom", 16);
constrainToParent(viewId, "start", 16);
constrainToParent(viewId, "end", 16);
constrainCenterHorizontally(viewId);
constrainCenterVertically(viewId);
constrainCenter(viewId);
constrainBelow(viewId, otherViewId, 8);
constrainAbove(viewId, otherViewId, 8);
constrainStartToEnd(viewId, otherId, 8);
constrainEndToStart(viewId, otherId, 8);
Łańcuchy (chains)
createHorizontalChain([btn1, btn2, btn3], "spread");
createVerticalChain([text1, text2, text3], "packed");
Guidelines
let guideline = createGuideline(constraintId, "horizontal", 0.5);
let guide2 = createGuidelineAbsolute(constraintId, "vertical", 100);
constrainToGuidelineTop(viewId, guideline, 8);
Barrier
let barrier = createBarrier(constraintId, "bottom", [view1, view2, view3]);
constrainBelow(button, barrier, 16);
Przykład
let container = createView("constraint", {width: "match", height: "match"});
addViewTo(root, container);
let title = createView("text", {text: "Nagłówek", textSize: 24});
let input = createView("input", {hint: "Email"});
let button = createView("button", {text: "Submit"});
addViewTo(container, title);
addViewTo(container, input);
addViewTo(container, button);
constrainCenterHorizontally(title);
constrainToParent(title, "top", 32);
constrainBelow(input, title, 16);
constrainToParent(input, "start", 16);
constrainToParent(input, "end", 16);
constrainBelow(button, input, 24);
constrainCenterHorizontally(button);