自定义布局:嵌套视图的大小调整为全屏
我正在创建一个自定义布局类,并且布局中的视图大小正确。 我遇到的问题是,如果我在该布局中包含另一种布局,则所有嵌套视图的子视图的大小基本上都是全屏大小。
XML 布局代码 - FrameLayout 中的内容大小调整不正确。
<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 代码。
@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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这最终成为足以满足我的需求的稳定代码。它仍然有错误,但是哦,好吧。
如果您需要其他引用的类,则整个项目都托管在此处。 http://code.google.com/p/motivatormaker-android/
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/