为什么静态片段的容器为空?

时间:2016-08-18 18:28:39

标签: android android-fragments

我将以下布局与静态片段和RelativeLayout作为父级:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_vertical_margin">

    <fragment
        android:id="@+id/list_fragment"
        android:name="com.listfragmenttest.MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

然而,容器或父容器在OnCreateView中出现null

public class MyListFragment extends ListFragment {

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

这是静态片段的正常行为吗?

1 个答案:

答案 0 :(得分:0)

是的,这就是它的实施方式。从XML静态膨胀的碎片得到null作为父容器。

有关详细信息,请参阅FragmentManager source。特别是,onCreateView()处理来自布局通胀的片段实例化,设置mInLayout = true并调用以performCreateView()为条件的mInLayout