Add android bindings to margin:auto
Summary: [This commit](1146013e9e
) (or diff D4501142) adds an `auto` option for margins. This diff allows you to leverage that in android via attribute `yoga:margin_all="auto"` (and as expected for the other edges).
Reviewed By: emilsjolander
Differential Revision: D4634684
fbshipit-source-id: 158f70ec975b5bb3a666e590b76eb52daeb38f49
This commit is contained in:
committed by
Facebook Github Bot
parent
3ef2970032
commit
b940fadb7e
@@ -14,7 +14,6 @@ import java.util.Map;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.content.res.Resources;
|
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
@@ -107,7 +106,7 @@ public class YogaLayout extends ViewGroup {
|
|||||||
* Adds a child view with the specified layout parameters.
|
* Adds a child view with the specified layout parameters.
|
||||||
*
|
*
|
||||||
* In the typical View is added, this constructs a {@code YogaNode} for this child and applies all
|
* In the typical View is added, this constructs a {@code YogaNode} for this child and applies all
|
||||||
* the {@code yoga:*} attributes. The Toga node is added to the Yoga tree and the child is added
|
* the {@code yoga:*} attributes. The Yoga node is added to the Yoga tree and the child is added
|
||||||
* to this ViewGroup.
|
* to this ViewGroup.
|
||||||
*
|
*
|
||||||
* If the child is a {@link YogaLayout} itself, we do not construct a new Yoga node for that
|
* If the child is a {@link YogaLayout} itself, we do not construct a new Yoga node for that
|
||||||
@@ -380,9 +379,9 @@ public class YogaLayout extends ViewGroup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < layoutParameters.attributes.size(); i++) {
|
for (int i = 0; i < layoutParameters.numericAttributes.size(); i++) {
|
||||||
final int attribute = layoutParameters.attributes.keyAt(i);
|
final int attribute = layoutParameters.numericAttributes.keyAt(i);
|
||||||
final float value = layoutParameters.attributes.valueAt(i);
|
final float value = layoutParameters.numericAttributes.valueAt(i);
|
||||||
|
|
||||||
if (attribute == R.styleable.yoga_align_content) {
|
if (attribute == R.styleable.yoga_align_content) {
|
||||||
node.setAlignContent(YogaAlign.fromInt(Math.round(value)));
|
node.setAlignContent(YogaAlign.fromInt(Math.round(value)));
|
||||||
@@ -568,6 +567,33 @@ public class YogaLayout extends ViewGroup {
|
|||||||
node.setWrap(YogaWrap.fromInt(Math.round(value)));
|
node.setWrap(YogaWrap.fromInt(Math.round(value)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < layoutParameters.stringAttributes.size(); i++) {
|
||||||
|
final int attribute = layoutParameters.stringAttributes.keyAt(i);
|
||||||
|
final String value = layoutParameters.stringAttributes.valueAt(i);
|
||||||
|
|
||||||
|
if (value.equals("auto")) {
|
||||||
|
if (attribute == R.styleable.yoga_margin_left) {
|
||||||
|
node.setMarginAuto(YogaEdge.LEFT);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_top) {
|
||||||
|
node.setMarginAuto(YogaEdge.TOP);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_right) {
|
||||||
|
node.setMarginAuto(YogaEdge.RIGHT);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_bottom) {
|
||||||
|
node.setMarginAuto(YogaEdge.BOTTOM);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_start) {
|
||||||
|
node.setMarginAuto(YogaEdge.START);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_end) {
|
||||||
|
node.setMarginAuto(YogaEdge.END);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_horizontal) {
|
||||||
|
node.setMarginAuto(YogaEdge.HORIZONTAL);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_vertical) {
|
||||||
|
node.setMarginAuto(YogaEdge.VERTICAL);
|
||||||
|
} else if (attribute == R.styleable.yoga_margin_all) {
|
||||||
|
node.setMarginAuto(YogaEdge.ALL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -608,7 +634,13 @@ public class YogaLayout extends ViewGroup {
|
|||||||
* like align_self (enums), the integer enum value is cast (rounding is used on the other side
|
* like align_self (enums), the integer enum value is cast (rounding is used on the other side
|
||||||
* to prevent precision errors). Dimension attributes are stored as float pixels.
|
* to prevent precision errors). Dimension attributes are stored as float pixels.
|
||||||
*/
|
*/
|
||||||
SparseArray<Float> attributes;
|
SparseArray<Float> numericAttributes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A mapping from attribute keys ({@code R.styleable.yoga_*}) with string values to those
|
||||||
|
* strings. This is used for values such as "auto".
|
||||||
|
*/
|
||||||
|
SparseArray<String> stringAttributes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a set of layout params from a source set. In the case that the source set is
|
* Constructs a set of layout params from a source set. In the case that the source set is
|
||||||
@@ -620,16 +652,18 @@ public class YogaLayout extends ViewGroup {
|
|||||||
public LayoutParams(ViewGroup.LayoutParams source) {
|
public LayoutParams(ViewGroup.LayoutParams source) {
|
||||||
super(source);
|
super(source);
|
||||||
if (source instanceof LayoutParams) {
|
if (source instanceof LayoutParams) {
|
||||||
attributes = ((LayoutParams) source).attributes.clone();
|
numericAttributes = ((LayoutParams) source).numericAttributes.clone();
|
||||||
|
stringAttributes = ((LayoutParams) source).stringAttributes.clone();
|
||||||
} else {
|
} else {
|
||||||
attributes = new SparseArray<>();
|
numericAttributes = new SparseArray<>();
|
||||||
|
stringAttributes = new SparseArray<>();
|
||||||
|
|
||||||
// Negative values include MATCH_PARENT and WRAP_CONTENT
|
// Negative values include MATCH_PARENT and WRAP_CONTENT
|
||||||
if (source.width >= 0) {
|
if (source.width >= 0) {
|
||||||
attributes.put(R.styleable.yoga_width, (float) width);
|
numericAttributes.put(R.styleable.yoga_width, (float) width);
|
||||||
}
|
}
|
||||||
if (source.height >= 0) {
|
if (source.height >= 0) {
|
||||||
attributes.put(R.styleable.yoga_height, (float) height);
|
numericAttributes.put(R.styleable.yoga_height, (float) height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -648,13 +682,14 @@ public class YogaLayout extends ViewGroup {
|
|||||||
*/
|
*/
|
||||||
public LayoutParams(int width, int height) {
|
public LayoutParams(int width, int height) {
|
||||||
super(width, height);
|
super(width, height);
|
||||||
attributes = new SparseArray<>();
|
numericAttributes = new SparseArray<>();
|
||||||
|
stringAttributes = new SparseArray<>();
|
||||||
// Negative values include MATCH_PARENT and WRAP_CONTENT
|
// Negative values include MATCH_PARENT and WRAP_CONTENT
|
||||||
if (width >= 0) {
|
if (width >= 0) {
|
||||||
attributes.put(R.styleable.yoga_width, (float) width);
|
numericAttributes.put(R.styleable.yoga_width, (float) width);
|
||||||
}
|
}
|
||||||
if (height >= 0) {
|
if (height >= 0) {
|
||||||
attributes.put(R.styleable.yoga_height, (float) height);
|
numericAttributes.put(R.styleable.yoga_height, (float) height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -667,15 +702,16 @@ public class YogaLayout extends ViewGroup {
|
|||||||
*/
|
*/
|
||||||
public LayoutParams(Context context, AttributeSet attrs) {
|
public LayoutParams(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
attributes = new SparseArray<>();
|
numericAttributes = new SparseArray<>();
|
||||||
|
stringAttributes = new SparseArray<>();
|
||||||
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.yoga);
|
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.yoga);
|
||||||
|
|
||||||
// Negative values include MATCH_PARENT and WRAP_CONTENT
|
// Negative values include MATCH_PARENT and WRAP_CONTENT
|
||||||
if (width >= 0) {
|
if (width >= 0) {
|
||||||
attributes.put(R.styleable.yoga_width, (float) width);
|
numericAttributes.put(R.styleable.yoga_width, (float) width);
|
||||||
}
|
}
|
||||||
if (height >= 0) {
|
if (height >= 0) {
|
||||||
attributes.put(R.styleable.yoga_height, (float) height);
|
numericAttributes.put(R.styleable.yoga_height, (float) height);
|
||||||
}
|
}
|
||||||
|
|
||||||
final int attributeCount = a.getIndexCount();
|
final int attributeCount = a.getIndexCount();
|
||||||
@@ -685,11 +721,13 @@ public class YogaLayout extends ViewGroup {
|
|||||||
a.getValue(attribute, val);
|
a.getValue(attribute, val);
|
||||||
|
|
||||||
if (val.type == TypedValue.TYPE_DIMENSION) {
|
if (val.type == TypedValue.TYPE_DIMENSION) {
|
||||||
attributes.put(
|
numericAttributes.put(
|
||||||
attribute,
|
attribute,
|
||||||
(float) a.getDimensionPixelSize(attribute, 0));
|
(float) a.getDimensionPixelSize(attribute, 0));
|
||||||
|
} else if (val.type == TypedValue.TYPE_STRING) {
|
||||||
|
stringAttributes.put(attribute, a.getString(attribute));
|
||||||
} else {
|
} else {
|
||||||
attributes.put(attribute, a.getFloat(attribute, 0));
|
numericAttributes.put(attribute, a.getFloat(attribute, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
a.recycle();
|
a.recycle();
|
||||||
|
@@ -91,15 +91,15 @@
|
|||||||
<enum name="space_around" value="4"/>
|
<enum name="space_around" value="4"/>
|
||||||
</attr>
|
</attr>
|
||||||
|
|
||||||
<attr name="margin_left" format="dimension"/>
|
<attr name="margin_left" format="dimension|string"/>
|
||||||
<attr name="margin_top" format="dimension"/>
|
<attr name="margin_top" format="dimension|string"/>
|
||||||
<attr name="margin_right" format="dimension"/>
|
<attr name="margin_right" format="dimension|string"/>
|
||||||
<attr name="margin_bottom" format="dimension"/>
|
<attr name="margin_bottom" format="dimension|string"/>
|
||||||
<attr name="margin_start" format="dimension"/>
|
<attr name="margin_start" format="dimension|string"/>
|
||||||
<attr name="margin_end" format="dimension"/>
|
<attr name="margin_end" format="dimension|string"/>
|
||||||
<attr name="margin_horizontal" format="dimension"/>
|
<attr name="margin_horizontal" format="dimension|string"/>
|
||||||
<attr name="margin_vertical" format="dimension"/>
|
<attr name="margin_vertical" format="dimension|string"/>
|
||||||
<attr name="margin_all" format="dimension"/>
|
<attr name="margin_all" format="dimension|string"/>
|
||||||
|
|
||||||
<attr name="margin_percent_left" format="dimension"/>
|
<attr name="margin_percent_left" format="dimension"/>
|
||||||
<attr name="margin_percent_top" format="dimension"/>
|
<attr name="margin_percent_top" format="dimension"/>
|
||||||
|
@@ -26,3 +26,7 @@ RTL locales are supported by default. That is, unless you explicitly set the `y
|
|||||||
## Attributes
|
## Attributes
|
||||||
|
|
||||||
The list of all attributes can be found in [attrs.xml](https://github.com/facebook/yoga/blob/master/android/sample/res/com/facebook/samples/yoga/res/values/attrs.xml), but logically map from the Yoga properties.
|
The list of all attributes can be found in [attrs.xml](https://github.com/facebook/yoga/blob/master/android/sample/res/com/facebook/samples/yoga/res/values/attrs.xml), but logically map from the Yoga properties.
|
||||||
|
|
||||||
|
## Auto margins
|
||||||
|
|
||||||
|
You can specify `margin_left="auto"` (or `margin_right` etc.) for auto values. This is in addition to the dimensions you can speicfy, such as `margin_left="20dp"`.
|
||||||
|
Reference in New Issue
Block a user