Skip to content

Instantly share code, notes, and snippets.

@b95505017
Created March 14, 2016 20:55
Show Gist options
  • Save b95505017/e9ebdd7fe99dd52c100a to your computer and use it in GitHub Desktop.
Save b95505017/e9ebdd7fe99dd52c100a to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.support.percent.PercentLayoutHelper;
import android.support.percent.PercentRelativeLayout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* Workaround for Issue 186498
*/
public class WorkaroundPercentRelativeLayout extends PercentRelativeLayout {
int mLastWidthMeasureSpec, mLastHeightMeasureSpec;
public WorkaroundPercentRelativeLayout(Context context) {
super(context);
}
public WorkaroundPercentRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WorkaroundPercentRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mLastHeightMeasureSpec = heightMeasureSpec;
mLastWidthMeasureSpec = widthMeasureSpec;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (oldh == 0 && oldw == 0) {
return;
}
boolean reMeasure = false;
for (int i = 0, N = getChildCount(); i < N; i++) {
View view = getChildAt(i);
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params instanceof PercentLayoutHelper.PercentLayoutParams) {
PercentLayoutHelper.PercentLayoutInfo info =
((PercentLayoutHelper.PercentLayoutParams) params).getPercentLayoutInfo();
if (info.widthPercent < 0 && info.heightPercent > 0 && info.aspectRatio > 0) {
if (params.width != 0) {
params.width = 0;
reMeasure = true;
}
}
if (info.heightPercent < 0 && info.widthPercent > 0 && info.aspectRatio > 0) {
if (params.height != 0) {
params.height = 0;
reMeasure = true;
}
}
}
}
if (reMeasure) {
measure(mLastWidthMeasureSpec, mLastHeightMeasureSpec);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment