Android 根据设备大小在主详细信息流中创建自定义列表视图
主详细信息流的基本模板使用Android 根据设备大小在主详细信息流中创建自定义列表视图,android,android-fragments,android-listview,Android,Android Fragments,Android Listview,主详细信息流的基本模板使用布尔mTwoPane值来确定是有两个窗格还是一个窗格。但是,它是在呈现ListFragment之后设置的。我想为小型设备创建一个ListView,该设备将在不同的活动中显示DetailFragment,并在设备上创建一个不同的ListView,将DetailFragment并排显示到ListFragment protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta
布尔mTwoPane
值来确定是有两个窗格还是一个窗格。但是,它是在呈现ListFragment
之后设置的。我想为小型设备创建一个ListView
,该设备将在不同的活动中显示DetailFragment
,并在设备上创建一个不同的ListView
,将DetailFragment
并排显示到ListFragment
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// rest of code
}
在确定mTwoPane
之后,我尝试使用setContentView(R.layout.activity\u item\u list)
,但是mTwoPane
始终保持为false。我还尝试过在if块前后使用setContentView(R.layout.activity\u item\u list)
两次,但应用程序崩溃了
任何帮助都将不胜感激。在您的布局中使用
框架布局
(而不是片段
),仅在if块
之后向框架布局
添加片段
去掉xml中的“
fragment
”标记,以保持对fragment
管理的完全控制。只有在if块将片段添加到FrameLayout之后,才能在布局中使用FrameLayout(而不是片段)。@DavidCorsalini-这很有效。谢谢。尽管我有一个疑问。在双窗格设备中,正确的ListView仅由FrameLayout的替换加载(没有在代码中向其添加片段)。为什么会发生这种情况?我认为xml中有一个片段标记。我会将此作为答案发布,接受它。