1

さまざまなオーバーレイされたImageViewを含むFrameLayoutがあります。

<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がレンダリングしている正確なピクセルの高さと幅を取得する方法はありますか?

4

1 に答える 1

5

これはあなたを助けるかもしれません

     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*/
        }

    });
}   
于 2013-01-25T06:59:44.497 に答える