Created
January 10, 2017 08:49
-
-
Save g4s8/cae5fa9af0cafe5fc2385de4d4ab59ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.com.equalsizelayout; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.os.Build; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.FrameLayout; | |
import java.util.HashMap; | |
import java.util.Map; | |
public final class EqualSizeLayout extends FrameLayout { | |
private final Map<Integer, View> childById = new HashMap<>(); | |
public EqualSizeLayout(Context context) { | |
super(context); | |
} | |
public EqualSizeLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public EqualSizeLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public EqualSizeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
@Override | |
public void onViewAdded(View child) { | |
super.onViewAdded(child); | |
final int viewId = child.getId(); | |
if (viewId != View.NO_ID) { | |
childById.put(viewId, child); | |
} | |
} | |
@Override | |
public void onViewRemoved(View child) { | |
super.onViewRemoved(child); | |
final int viewId = child.getId(); | |
if (viewId != View.NO_ID) { | |
childById.remove(viewId); | |
} | |
} | |
@Override | |
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) { | |
super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed); | |
if (child.getVisibility() == GONE) { | |
return; | |
} | |
final LayoutParams lp = LayoutParams.class.cast(child.getLayoutParams()); | |
int height = -1; | |
int width = -1; | |
if (childById.containsKey(lp.equalHeightTo)) { | |
height = childById.get(lp.equalHeightTo).getMeasuredHeight(); | |
} | |
if (childById.containsKey(lp.equalWidthTo)) { | |
width = childById.get(lp.equalWidthTo).getMeasuredWidth(); | |
} | |
boolean changed = false; | |
if (height > 0 && lp.height != height) { | |
lp.height = height; | |
changed = true; | |
} | |
if (width > 0 && lp.width != width) { | |
lp.width = width; | |
changed = true; | |
} | |
if (changed) { | |
child.setLayoutParams(lp); | |
} | |
} | |
@Override | |
protected FrameLayout.LayoutParams generateDefaultLayoutParams() { | |
return new LayoutParams(super.generateDefaultLayoutParams()); | |
} | |
@Override | |
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { | |
return new LayoutParams(lp); | |
} | |
@Override | |
public FrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs) { | |
return new LayoutParams(getContext(), attrs); | |
} | |
public static final class LayoutParams extends FrameLayout.LayoutParams { | |
private final int equalHeightTo; | |
private final int equalWidthTo; | |
private LayoutParams(Context c, AttributeSet attrs) { | |
super(c, attrs); | |
final TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.EqualSizeLayout_Layout); | |
equalHeightTo = a.getResourceId(R.styleable.EqualSizeLayout_Layout_layout_equalHeightTo, 0); | |
equalWidthTo = a.getResourceId(R.styleable.EqualSizeLayout_Layout_layout_equalWidthTo, 0); | |
a.recycle(); | |
} | |
public LayoutParams(int width, int height) { | |
super(width, height); | |
equalHeightTo = View.NO_ID; | |
equalWidthTo = View.NO_ID; | |
} | |
public LayoutParams(int width, int height, int gravity) { | |
super(width, height, gravity); | |
equalHeightTo = View.NO_ID; | |
equalWidthTo = View.NO_ID; | |
} | |
public LayoutParams(ViewGroup.LayoutParams source) { | |
super(source); | |
equalHeightTo = View.NO_ID; | |
equalWidthTo = View.NO_ID; | |
} | |
public LayoutParams(MarginLayoutParams source) { | |
super(source); | |
equalHeightTo = View.NO_ID; | |
equalWidthTo = View.NO_ID; | |
} | |
public LayoutParams(FrameLayout.LayoutParams source) { | |
super(source); | |
equalHeightTo = View.NO_ID; | |
equalWidthTo = View.NO_ID; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment