![](/img/trans.png)
[英]How to use the Android Navigation component (Nav Graph) in a Drawer Layout (with navigation drawer Menu)?
[英]How to use Navigation Component in Customized Layout?
我正在使一個應用程序看起來像這樣。
圓形的東西是紐扣。 綠色部分是片段。
我的host_navigation.xml
由A,B,C片段組成。
這些片段屬於MainActivity
(當然,它具有fragment
視圖),並且沒有<action>
(設計中的片段之間沒有行),因為我想隨機打開每個片段。 例如,A-> B,B-> A C-> A,...無論您身在何處,都可以移動到特定片段。
三個圓形按鈕也屬於MainActivity
。
我找到了BottomNavigationView
或NavigationView
示例。 但是,它們似乎無法在此布局中使用。 但是行為是非常相似的。 只是布局不同。
我該如何實現,也可以自然地管理堆棧行為? 例如,返回按鈕,我想保留最后3個片段,並刪除堆棧中所有先前的片段。
+
這是我的host_navigation.xml
(沒有動作。)
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/host_navigation"
app:startDestination="@id/aFragment">
<fragment android:id="@+id/aFragment"
android:name="com.example.AFragment"
android:label="fragment_a"
tools:layout="@layout/fragment_a"/>
<fragment android:id="@+id/bFragment"
android:name="com.example.BFragment"
android:label="fragment_b"
tools:layout="@layout/fragment_b"/>
<fragment android:id="@+id/cFragment"
android:name="com.example.CFragment"
android:label="fragment_c"
tools:layout="@layout/fragment_c">
</fragment>
</navigation>
這是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/rl_tab_area"
android:layout_width="match_parent"
android:layout_height="105dp">
<Button
android:id="@+id/btn_a"
android:layout_width="50dp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:src="@drawable/ic_a"
android:margin="5dp"
android:background="@drawable/btn_a"
/>
<Button
android:id="@+id/btn_b"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/btn_a"
android:background="@drawable/btn_b"/>
<Button
android:id="@+id/btn_c"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:background="@drawable/btn_c"/>
</RelativeLayout>
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_below="@+id/rl_tab_area"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/host_navigation">
</fragment>
</RelativeLayout>
我想我需要在MainActivity
使用它
val navController = Navigation.findNavController(this@MainActivity, R.id.host_navigation)
如果使用NavigaitonBottomView
然后,我可以將此值傳遞給它,但現在不知道,因為我不使用它。
我試圖以此稱呼其他片段
Navigation.findNavController(this, R.id.fragment).navigate(R.id.aFragment)
並嘗試
Navigation.findNavController(this, R.id.host_navigation).navigate(R.id.aFragment)
Navigation.findNavController(R.id.host_navigation).navigate(R.id.aFragment)
findNavController(R.id.host_navigation).navigate(R.id.aFragment)
還有其他方式。 但是到目前為止,他們都沒有工作。
通過以下方式為所有片段設置操作:
<navigation
...
<fragment
android:id="@+id/fragment_a"
android:label="fragment_a"
android:name="com.example.AFragment"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/a_to_b"
app:destination="@id/fragment_b"
app:launchSingleTop="true"/>
<action
android:id="@+id/a_to_c"
app:destination="@id/fragment_c"
app:launchSingleTop="true"/>
</fragment>
</navigation>
或者相反,您可以使用app:popUpTo
然后通過navController啟動它們:
navController.navigate(FragmentAFragmentDirections.aToC())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.