具有自定义视图层次结构的 ListFragment 在 onViewCreated 中的宽度/高度为零 children

ListFragment with a custom view hierarchy results with zero width / height children in onViewCreated

我正在为 ListFragment 膨胀 View,其中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

public void onViewCreated(View view, Bundle savedInstanceState) 中,我正在检查视图 children 的 width/height,它们似乎始终为 0。为什么?

这是我的 fragment_home.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/home_fragment_map"
        android:layout_width="match_parent"
        android:layout_height="@dimen/home_map_h" />

    <LinearLayout
        android:id="@id/android:empty"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="@dimen/event_height"
        android:gravity="center">

        <FrameLayout
            android:layout_width="50dp"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/location_icon_grey"
                android:layout_gravity="center" />

        </FrameLayout>

        <com.gigaset.location.ui.custom.TextViewMuseo
            android:layout_width="match_parent"
            android:text="@string/home_no_events"
            android:layout_height="@dimen/home_latest_pos_h"
            android:gravity="center_vertical" />

    </LinearLayout>

    <ListView
        android:id="@id/android:list"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Views 在布局到屏幕上之前不会显示任何大小。又名,通常在 onResume() 之后。如果您希望在已知时获得它的测量值,您可以使用布局侦听器。有关更多详细信息和示例:Android - get height of a view before it´s drawn