[英]Activity can be used for Navigation Drawer or Should I stick to Fragments?
[英]How can I use Fragments to open items on a Drawerlayout, when i used the Activity Drawer Navigation Template that provides android studio?
在activity_main.xml
嘗試替換:
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
有了這個:
<FrameLayout android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后使用R.id.fragment_container
作為您的R.id.x
但是,這將用FAB和協調器布局替換視圖。 但是您可能希望將它們添加到所需的片段中。
如果不是,您可以嘗試替換下面在app_bar_main.xml
找到的xml, 或將其包裝在 FrameLayout
。
<include layout="@layout/content_main"/>
更新:
替換為activity_main.xml
會導致一些問題,因為這會刪除工具欄。
如果替換app_bar_main.xml
的include, app_bar_main.xml
片段很可能會被頂部的工具欄重疊。 為防止這種情況,您可以嘗試在上面的FrameLayout
代碼段中添加app:layout_behavior="@string/appbar_scrolling_view_behavior"
。
R.id.x是要替換您的片段的View的ID。 因此,在布局R.layout.xyz中,您已經創建了一個布局
<FrameLayout android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
並將片段添加/替換為該ID。 (R.id.fragment_container),以便將新片段放置在該布局中。
transaction.add(R.id.fragment_container, firstFragment)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.