Source code
package android.support.v7.internal.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewCompat;
import android.support.v7.appcompat.R;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;
public class ButtonBarLayout extends LinearLayout {
private boolean mAllowStacking;
private int mLastWidthSize = -1;
public ButtonBarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ButtonBarLayout);
this.mAllowStacking = ta.getBoolean(R.styleable.ButtonBarLayout_allowStacking, false);
ta.recycle();
}
public void setAllowStacking(boolean allowStacking) {
if (this.mAllowStacking != allowStacking) {
this.mAllowStacking = allowStacking;
if (!this.mAllowStacking && getOrientation() == 1) {
setStacked(false);
}
requestLayout();
}
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int initialWidthMeasureSpec;
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (this.mAllowStacking) {
if (widthSize > this.mLastWidthSize && isStacked()) {
setStacked(false);
}
this.mLastWidthSize = widthSize;
}
boolean needsRemeasure = false;
if (isStacked() || MeasureSpec.getMode(widthMeasureSpec) != 1073741824) {
initialWidthMeasureSpec = widthMeasureSpec;
} else {
initialWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, Integer.MIN_VALUE);
needsRemeasure = true;
}
super.onMeasure(initialWidthMeasureSpec, heightMeasureSpec);
if (this.mAllowStacking && !isStacked() && (getMeasuredWidthAndState() & ViewCompat.MEASURED_STATE_MASK) == ViewCompat.MEASURED_STATE_TOO_SMALL) {
setStacked(true);
needsRemeasure = true;
}
if (needsRemeasure) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
private void setStacked(boolean stacked) {
setOrientation(stacked ? 1 : 0);
setGravity(stacked ? 5 : 80);
View spacer = findViewById(R.id.spacer);
if (spacer != null) {
spacer.setVisibility(stacked ? 8 : 4);
}
for (int i = getChildCount() - 2; i >= 0; i--) {
bringChildToFront(getChildAt(i));
}
}
private boolean isStacked() {
return getOrientation() == 1;
}
}