我有以下布局,其中静态 fragment 和 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);
}
}
这是静态 fragment 的正常行为吗?
最佳答案
是的,这就是它的实现方式。从 XML 静态膨胀的 fragment 得到 null
作为父容器。
详情咨询 FragmentManager
source .特别是,onCreateView()
处理布局膨胀的 fragment 实例化,设置mInLayout = true
并调用 performCreateView()
以 mInLayout
为条件.
关于android - 为什么静态 fragment 的容器为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39025151/