为什么我的FrameLayout有0高?

时间:2013-01-25 06:49:08

标签: android

我有一个FrameLayout,包含各种重叠的ImageViews:

<FrameLayout
    android:id="@+id/australia_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/australia"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/map_australia"
        />
    <ImageView
        android:id="@+id/nsw"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/map_nsw"
        android:visibility="invisible"
        />
    <ImageView
        android:id="@+id/victoria"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/map_victoria"
        android:visibility="invisible"
        />
        ...etc...
</FrameLayout>

在我的代码中,我尝试渲染我的FrameLayout:

final Dialog dialog = new Dialog((Context) parent);
dialog.setContentView(R.layout.dialog_region);

FrameLayout fl = (FrameLayout) dialog.findViewById(R.id.australia_frame);
final int height = fl.getHeight();
Validate.isTrue(height > 0);

我的代码崩溃了:

java.lang.IllegalArgumentException: The validated expression is false
    at org.apache.commons.lang3.Validate.isTrue(Validate.java:180)

崩溃的行是Validate.isTrue(height > 0);

为什么我的FrameLayout没有高度?有没有办法获得我的FrameLayout渲染的精确px高度和宽度?

1 个答案:

答案 0 :(得分:4)

这可能对您有所帮助

     FrameLayout target = (FrameLayout) dialog.findViewById(R.id.australia_frame);
    target.post(new Runnable() {

        @Override
        public void run() {             
            int width = target.getWidth();
                            int height = width/2;
            /*your code with width and height*/
        }

    });
}