简体   繁体   English

当我使用提供Android Studio的活动抽屉导航模板时,如何使用片段在Drawerlayout上打开项目?

[英]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. 最后,使用他们提供给我的答案中的一些建议解决了问题。

  1. Change content_main.xml content. 更改content_main.xml的内容。 Using a FrameLayout with its correspodiente id. 使用带有其对应ID的FrameLayout。

content_main.xml

  1. Add fragment on onNavigationItemSelected(MenuItem item) 在onNavigationItemSelected(MenuItem item)上添加片段

添加片段

  1. Result 结果

When I press a menu item ... 当我按下菜单项时...

菜单

...the fragment is displayed ...显示片段

分段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 活动可以用于导航抽屉,还是应该坚持使用片段? - Activity can be used for Navigation Drawer or Should I stick to Fragments? 如何在 DrawerLayout 中正确使用 Android Fragments? - How can I use correctly Android Fragments in DrawerLayout? 如何在没有碎片的情况下使用导航抽屉? - How can I use the Navigation Drawer without fragments? 如何使用导航抽屉嵌套碎片? - How can I nest fragments with a navigation drawer? 我可以在Android Studio中预览打开的导航抽屉吗? - Can I preview an open Navigation Drawer in Android Studio 如何在android活动中添加顶部抽屉和底部导航? - How can I add top drawer and bottom navigation in android activity? 在新的导航抽屉活动模板中使用onNavigationItemSelected在片段之间切换(Android Studio 1.4以后) - Switch between Fragments with onNavigationItemSelected in new Navigation Drawer Activity template (Android Studio 1.4 onward) Jetpack 导航:在片段之间导航时,如何使用与启动新活动时相同的动画? - Jetpack Navigation: When navigating between fragments, how can I use the same animation as when launching a new activity? 如何让导航抽屉在android studio上打开一个活动 - how to make navigation drawer open an activity on android studio 使用导航抽屉时是否必须使用碎片 - Do i have to use fragments when using navigation drawer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM