android - 为什么静态 fragment 的容器为空?

标签 android android-fragments

我有以下布局,其中静态 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/

相关文章:

android - 定义位图字节数并设置位图缓冲区的限制

android - 按下切换按钮 3 次后,闪光灯 Android 应用程序崩溃,并且 LED 永远不会亮起

java - Robolectric 单元测试自定义布局上的 ClassCastException

android - 可以只用一个实例创建 fragment 吗

android - Android Intent 对其 URI 做了什么?

Android 禁用 webview 滚动但不是内部 ScrollView

java - Android Canvas.DrawBitmap 没有模糊/抗锯齿?

java - 从工具栏按回时 Viewpager 将无法工作

android setText 使我的应用程序崩溃

android - 在 Fragment 级别应用的滑动手势以及禁用默认滑动的 ViewPager