I’m creating a custom layout class and the views within the layout are sizing properly.
The problem I’m having is that if I include another layout within that one the size of all the nested view’s children are basically full screen sizes.
XML Layout Code – The contents within the FrameLayout are not sizing properly.
<CustomLayout>
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content"/>
<FrameLayout android:layout_height="wrap_content" android:layout_width="wrap_content">
<!-- The views in here are sizing themselves as full screen. -->
<include layout="@layout/panel_emptypicture" android:id="@+id/EmptyPictureRelativeLayout" android:visibility="visible" />
</FrameLayout>
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" />
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" />
</CustomLayout>
Java Code for custom layout.
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int numChildren = getChildCount();
Dimension d = new Dimension(0, 0, this.getWidth(), this.getHeight());
final float scaleX = this.getWidth() / virtualWidth;
final float scaleY = this.getHeight() / virtualHeight;
for(int i = 0; i < numChildren; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final Dimension lpd = lp.getDimension();
Dimension dest = lpd.scale(scaleX, scaleY, d);
child.layout(dest.x, dest.y, dest.width + dest.x, dest.height + dest.y);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width, height;
float maxWidth = MeasureSpec.getSize(widthMeasureSpec);
float maxHeight = MeasureSpec.getSize(heightMeasureSpec);
final float widthVirtualMultiplier = virtualWidth / virtualHeight;
final float heightVirtualMultiplier = virtualHeight / virtualWidth;
if(maxWidth * heightVirtualMultiplier < maxHeight) {
width = (int)maxWidth;
height = (int) (width * heightVirtualMultiplier);
} else {
height = (int)maxHeight;
width = (int)(height * widthVirtualMultiplier);
}
setMeasuredDimension(width, height);
// Tried adding this to see if it would do anything. Nope, no effect.
final int numChildren = getChildCount();
for(int i = 0; i < numChildren; i++) {
final View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
This ended up being the code that was stable enough for my needs. It still had the bug but oh well.
The entire project is hosted here if you need the other referenced classes. http://code.google.com/p/motivatormaker-android/