Create a gist now

Instantly share code, notes, and snippets.

An extended RelativeLayout that fixes layout issues when the size of the layout is inexact (that is, wrap_content) and you want to base the positioning of one or more child views on the positioning of another child view that is itself positioned by layout_centerInParent, layout_centerHorizontal or layout_centerVertical. It is clearly not efficie…
/*
* Copyright (C) 2013 Scott Weber
*
* 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
*
* http://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.scottweber.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class DoubleMeasureRelativeLayout extends RelativeLayout {
public DoubleMeasureRelativeLayout(Context context) {
super(context);
}
public DoubleMeasureRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DoubleMeasureRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY
|| MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY) {
super.onMeasure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment