Android 扩展框架布局,赢得子视图';我再也看不到了
这是我的带有Android 扩展框架布局,赢得子视图';我再也看不到了,android,android-layout,Android,Android Layout,这是我的带有FrameLayout的代码: <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/frameView" android:layout_width="fill_parent" android:lay
FrameLayout
的代码:
<FrameLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView
android:id="@+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image1"
/>
</FrameLayout>
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// let the super class handle calculations
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// set again the dimensions, this time calculated as we want
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() / 2);
// this is required because the children keep the super class calculated dimensions (which will not work with the new MyFrameLayout sizes)
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View v = getChildAt(i);
// this works because you set the dimensions of the ImageView to FILL_PARENT
v.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(),
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
getMeasuredHeight(), MeasureSpec.EXACTLY));
}
}
然后在xml中使用它:
<com.test.MyFrameLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView
android:id="@+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image1"
/>
</com.test.MyFrameLayout>
但现在,内部图像视图消失了
我认为我没有正确地实施onMeasure
或onLayout
。我想我也需要调整儿童视图的大小。但我不知道现在该怎么办
更新
根据图像的注释,我只是检查了我的代码并更新了我的问题。谢谢我想在您的
onMeasure
方法中,您忘记了对所有子视图调用onMeasure,包括您的ImageView。您必须浏览所有的子视图,并对它们进行如下测量:
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).measure(....);
}
for(int i=0;i
您看不到任何子视图,因为您没有正确覆盖测量上的方法
。您可以让超类实现该逻辑(这样子视图就会显示),然后为MyFrameLayout
重置计算的宽度和高度:
<FrameLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView
android:id="@+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image1"
/>
</FrameLayout>
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// let the super class handle calculations
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// set again the dimensions, this time calculated as we want
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight() / 2);
// this is required because the children keep the super class calculated dimensions (which will not work with the new MyFrameLayout sizes)
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View v = getChildAt(i);
// this works because you set the dimensions of the ImageView to FILL_PARENT
v.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(),
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
getMeasuredHeight(), MeasureSpec.EXACTLY));
}
}
@覆盖
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
//让超类处理计算
超级测量(宽度测量、高度测量);
//再次设置尺寸,这次按照我们的要求计算
setMeasuredDimension(getMeasuredWidth(),getMeasuredHeight()/2);
//这是必需的,因为子级保留超级类计算的尺寸(这不适用于新的MyFrameLayout尺寸)
最终整数计数=getChildCount();
for(int i=0;i
如果您发布的布局不是完整的布局,那么我的代码可能无法工作。最简单的方法是在onMeasure中更改度量值,但仍然调用super:
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) (width * 0.5);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
通过这种方式,您可以获得所需的尺寸,但不必手动测量子对象 两点建议:一,。在
MyFrameLayout
类中检查您的方法onMeasure
和onLayout
。2.尝试使用hierarhyviewer工具进行调试。另外,如果可能的话,可以显示您自定义的所有代码layout@TheDimasig,我刚刚更新了我的问题,请回顾:)我刚刚阅读了FrameLayout
的源代码,onMeasure
和onLayout
看起来相当复杂,有没有一种简单的方法可以做到这一点?而不是覆盖所有的方法体和setDimension()尝试与您的参数一起调用super.onMeasure(widthMeasureSpec*0.5,heightMeasureSpec)。此解决方案可能也需要覆盖onLayout,我尝试了widthMeasureSpec*0.5
,但最终尺寸看起来与预期不符。我搞不懂,你有时间的时候能给我一个工作的例子吗?不幸的是不行。如果super.onMeasure调用的技巧不起作用,我担心你需要实现你自己的onMeasure和onLayout,不要忘记childs,祝你好运=)我可以确认它有效。如果不调用super.onMeasure(),子视图将不可见!非常感谢你们,我已经读了很多讨论,这是唯一有效的答案,使孩子的大小与家长布局!