/* * Copyright 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.material.internal; import com.google.android.material.R; import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.drawable.Drawable; import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.IntDef; import android.support.annotation.Nullable; import android.support.annotation.RestrictTo; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.util.SparseIntArray; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.RelativeLayout; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; import java.util.List; /** * A layout that arranges its children in a way its attributes can be specified like the CSS * Flexible Box Layout Module. This class extends the {@link ViewGroup} like other layout classes * such as {@link LinearLayout} or {@link RelativeLayout}, the attributes can be specified from a * layout XML or from code. * *
The supported attributes that you can use are: * *
And for the children of the FlexboxLayout, you can use: * *
Note that some parent fields (which are not primitive nor a class implements {@link
* Parcelable}) are not included as the stored/restored fields after this class is
* serialized/de-serialized as an {@link Parcelable}.
*/
public static class LayoutParams extends ViewGroup.MarginLayoutParams implements FlexItem {
/** @see FlexItem#getOrder() */
private int order = FlexItem.ORDER_DEFAULT;
/** @see FlexItem#getFlexGrow() */
private float flexGrow = FlexItem.FLEX_GROW_DEFAULT;
/** @see FlexItem#getFlexShrink() */
private float flexShrink = FlexItem.FLEX_SHRINK_DEFAULT;
/** @see FlexItem#getFlexBasisPercent() */
private float flexBasisPercent = FlexItem.FLEX_BASIS_PERCENT_DEFAULT;
/** @see FlexItem#getMinWidth() */
private int minWidth;
/** @see FlexItem#getMinHeight() */
private int minHeight;
/** @see FlexItem#getMaxWidth() */
private int maxWidth = MAX_SIZE;
/** @see FlexItem#getMaxHeight() */
private int maxHeight = MAX_SIZE;
/** @see FlexItem#isWrapBefore() */
private boolean wrapBefore;
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlexboxLayout_Layout);
order = a.getInt(R.styleable.FlexboxLayout_Layout_layout_order, ORDER_DEFAULT);
flexGrow = a.getFloat(R.styleable.FlexboxLayout_Layout_layout_flexGrow, FLEX_GROW_DEFAULT);
flexShrink =
a.getFloat(R.styleable.FlexboxLayout_Layout_layout_flexShrink, FLEX_SHRINK_DEFAULT);
flexBasisPercent =
a.getFraction(
R.styleable.FlexboxLayout_Layout_layout_flexBasisPercent,
1,
1,
FLEX_BASIS_PERCENT_DEFAULT);
minWidth = a.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_minWidth, 0);
minHeight = a.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_minHeight, 0);
maxWidth =
a.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_maxWidth, MAX_SIZE);
maxHeight =
a.getDimensionPixelSize(R.styleable.FlexboxLayout_Layout_layout_maxHeight, MAX_SIZE);
wrapBefore = a.getBoolean(R.styleable.FlexboxLayout_Layout_layout_wrapBefore, false);
a.recycle();
}
public LayoutParams(LayoutParams source) {
super(source);
order = source.order;
flexGrow = source.flexGrow;
flexShrink = source.flexShrink;
flexBasisPercent = source.flexBasisPercent;
minWidth = source.minWidth;
minHeight = source.minHeight;
maxWidth = source.maxWidth;
maxHeight = source.maxHeight;
wrapBefore = source.wrapBefore;
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(int width, int height) {
super(new ViewGroup.LayoutParams(width, height));
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
protected LayoutParams(Parcel in) {
// Passing a resolved value to resolve a lint warning
// height and width are set in this method anyway.
super(0, 0);
this.order = in.readInt();
this.flexGrow = in.readFloat();
this.flexShrink = in.readFloat();
this.flexBasisPercent = in.readFloat();
this.minWidth = in.readInt();
this.minHeight = in.readInt();
this.maxWidth = in.readInt();
this.maxHeight = in.readInt();
this.wrapBefore = in.readByte() != 0;
this.bottomMargin = in.readInt();
this.leftMargin = in.readInt();
this.rightMargin = in.readInt();
this.topMargin = in.readInt();
this.height = in.readInt();
this.width = in.readInt();
}
@Override
public int getWidth() {
return width;
}
@Override
public void setWidth(int width) {
this.width = width;
}
@Override
public int getHeight() {
return height;
}
@Override
public void setHeight(int height) {
this.height = height;
}
@Override
public int getOrder() {
return order;
}
@Override
public void setOrder(int order) {
this.order = order;
}
@Override
public float getFlexGrow() {
return flexGrow;
}
@Override
public void setFlexGrow(float flexGrow) {
this.flexGrow = flexGrow;
}
@Override
public float getFlexShrink() {
return flexShrink;
}
@Override
public void setFlexShrink(float flexShrink) {
this.flexShrink = flexShrink;
}
@Override
public int getMinWidth() {
return minWidth;
}
@Override
public void setMinWidth(int minWidth) {
this.minWidth = minWidth;
}
@Override
public int getMinHeight() {
return minHeight;
}
@Override
public void setMinHeight(int minHeight) {
this.minHeight = minHeight;
}
@Override
public int getMaxWidth() {
return maxWidth;
}
@Override
public void setMaxWidth(int maxWidth) {
this.maxWidth = maxWidth;
}
@Override
public int getMaxHeight() {
return maxHeight;
}
@Override
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
@Override
public boolean isWrapBefore() {
return wrapBefore;
}
@Override
public void setWrapBefore(boolean wrapBefore) {
this.wrapBefore = wrapBefore;
}
@Override
public float getFlexBasisPercent() {
return flexBasisPercent;
}
@Override
public void setFlexBasisPercent(float flexBasisPercent) {
this.flexBasisPercent = flexBasisPercent;
}
@Override
public int getMarginLeft() {
return leftMargin;
}
@Override
public int getMarginTop() {
return topMargin;
}
@Override
public int getMarginRight() {
return rightMargin;
}
@Override
public int getMarginBottom() {
return bottomMargin;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.order);
dest.writeFloat(this.flexGrow);
dest.writeFloat(this.flexShrink);
dest.writeFloat(this.flexBasisPercent);
dest.writeInt(this.minWidth);
dest.writeInt(this.minHeight);
dest.writeInt(this.maxWidth);
dest.writeInt(this.maxHeight);
dest.writeByte(this.wrapBefore ? (byte) 1 : (byte) 0);
dest.writeInt(this.bottomMargin);
dest.writeInt(this.leftMargin);
dest.writeInt(this.rightMargin);
dest.writeInt(this.topMargin);
dest.writeInt(this.height);
dest.writeInt(this.width);
}
public static final Parcelable.Creator