本文介绍了为什么我的FrameLayout有0高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a FrameLayout, containing various overlayed 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>

在我的code我尝试和渲染我的FrameLayout:

In my code I try and render my 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);

我的code。与崩溃:

My code crashes with:

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

这是崩溃的线是 Validate.isTrue(高度大于0);

为什么我的FrameLayout没有一个高度?有没有办法得到确切的像素高度和宽度,我的FrameLayout是在渲染?

Why does my FrameLayout not have a height? Is there a way to get the exact px height and width that my FrameLayout is rendering at?

推荐答案

这可能会帮助你。

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

    });
}   

这篇关于为什么我的FrameLayout有0高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:27