[英]How can I use Fragments to open items on a Drawerlayout, when i used the Activity Drawer Navigation Template that provides android studio?
Template that provide Android Studio 提供Android Studio的模板
This is how I try to do. 这就是我试图做的。 What is the R.id.x?.
什么是R.id.x? I tried using layout id in xml ActivityMain but not the fragment is displayed or overlaid with out activity information.
我尝试在xml ActivityMain中使用布局ID,但未显示片段或将其覆盖有活动信息。
After selecting one of the items, the fragment will be displayed above the activity. 选择其中一项后,片段将显示在活动上方。 The activity has several elements belonging to material design as the FAB or the Toolbar, Could this create problems with what I do?
该活动有几个属于材料设计的元素,例如FAB或工具栏,这会给我的工作造成麻烦吗?
In activity_main.xml
try replacing: 在
activity_main.xml
尝试替换:
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
With this: 有了这个:
<FrameLayout android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And then use R.id.fragment_container
as your R.id.x
. 然后使用
R.id.fragment_container
作为您的R.id.x
This will however replace the view with the FAB and coordinator layout. 但是,这将用FAB和协调器布局替换视图。 But you will probably want to add them to the fragments where you want them.
但是您可能希望将它们添加到所需的片段中。
If not you could try replacing the xml found below in app_bar_main.xml
, or wrap it in the FrameLayout
.如果不是,您可以尝试替换下面在
app_bar_main.xml
找到的xml, 或将其包装在 FrameLayout
。
<include layout="@layout/content_main"/>
Update: 更新:
Replacing in activity_main.xml
will cause some issues as that removes the toolbar. 替换为
activity_main.xml
会导致一些问题,因为这会删除工具栏。
If you replace the include in app_bar_main.xml
your fragments will most likely get overlapped by the toolbar at the top. 如果替换
app_bar_main.xml
的include, app_bar_main.xml
片段很可能会被顶部的工具栏重叠。 To prevent this you could try adding app:layout_behavior="@string/appbar_scrolling_view_behavior"
in the FrameLayout
snippet above. 为防止这种情况,您可以尝试在上面的
FrameLayout
代码段中添加app:layout_behavior="@string/appbar_scrolling_view_behavior"
。
R.id.x is the id of the View that your fragment going to be replaced. R.id.x是要替换您的片段的View的ID。 So in your layout R.layout.xyz you have created a layout
因此,在布局R.layout.xyz中,您已经创建了一个布局
<FrameLayout android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and add/replace the fragment to that id. 并将片段添加/替换为该ID。 (R.id.fragment_container) such that your new fragment will be placed in that layout.
(R.id.fragment_container),以便将新片段放置在该布局中。
transaction.add(R.id.fragment_container, firstFragment)
At the end solved the problem using some of the recommendations made in the answers they provided me. 最后,使用他们提供给我的答案中的一些建议解决了问题。
When I press a menu item ... 当我按下菜单项时...
...the fragment is displayed ...显示片段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.