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

时间:2015-03-06 10:52:42

标签: android android-layout listview android-fragments android-inflate

我用ListFragmentView充气:

@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)我查看视图儿童的宽度/高度,看起来他们总是为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>

1 个答案:

答案 0 :(得分:0)

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