我有一个UI,我需要一个片段,以横向模式显示(视图),但不是在纵向模式。在纵向模式下,它仍然可用,但会使用ListPopupWindow显示其结果。
我想我可以通过使用< fragment />标记为横向布局,同时以编程方式创建片段(如果未开始)(在我们是纵向的情况下)。
只要您从横向开始,只要您从头开始,以编程方式创建片段,当您尝试将该片段附加到布局时旋转仿真器时,应用程序将崩溃:
java.lang.IllegalStateException:
Fragment did not create a view.
at
android.app.Activity.onCreateView(Activity.java:4095)
Fragment.isInLayout()的文档似乎暗示它应该能够这样处理:
Return true if the layout is included
as part of an activity view hierarchy
via the tag. This will
always be true when fragments are
created through the tag,
except in the case where an old
fragment is restored from a previous
state and it does not appear in the
layout of the current state.
所以问题是如何正确地做到这一点,还是有缺点的东西?
更新:
像isInLayout()似乎不像现在那样行事。如果您已经手动将容器添加到容器中,则返回false。
另外,如果您将片段手动添加到容器中,然后将(设备)旋转到不包含该布局的布局,则它将崩溃:
Caused by:
java.lang.IllegalArgumentException: No
view found for id 0x7f060011 for
fragment SearchFragment{4042f868 #2
id=0x7f060011 SearchFragment} at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:722)