[英]Navigation drawer showing current destination name in title
我已經開發成我的應用程序簡單的導航抽屜機制。 有關應用程序中使用的代碼的更多信息: https://developer.android.com/guide/navigation/navigation-ui
問題是當用戶在目標工具欄的標題之間切換時,它會顯示當前位置 xml 布局名稱:
重新啟動或啟動應用程序時,當前標題是清單中指定的,當切換到其他目的地時,它會再次更改。
主要活動、布局和Kotlin的代碼:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
tools:openDrawer="start"
tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.CurrencyApp.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/Theme.CurrencyApp.PopupOverlay">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/color_mode_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|end"
android:text="@string/action_color_mode"
android:textAppearance="@style/TextAppearance.AppCompat.Small" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/nav_header"
android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>
Kotlin 文件:
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
private lateinit var prefs : SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
val navController = findNavController(R.id.fragment)
val drawerLayout = binding.drawerLayout
val appBarConfiguration = AppBarConfiguration(navGraph = navController.graph,drawerLayout)
binding.toolbar.setupWithNavController(navController,appBarConfiguration)
導航抽屜 header:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="176dp"
android:background="@color/purple_200"
android:gravity="bottom"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="Testowy nav drawer"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="info@codingflow.com"/>
</LinearLayout>
導航抽屜菜單:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_message"
android:icon="@drawable/ic_add_circle_outline_24px"
android:title="Message" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:title="Share"
android:icon="@drawable/ic_add" />
</menu>
</item>
</menu>
導航文件:
<?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/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.example.currencyapp.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main" >
<action
android:id="@+id/main_to_tab"
app:destination="@id/tabFragment" />
<action
android:id="@+id/main_to_card"
app:destination="@id/recyclerFragment2" />
</fragment>
<fragment
android:id="@+id/recyclerFragment2"
android:name="com.example.currencyapp.RecyclerFragment"
android:label="fragment_recycler"
tools:layout="@layout/fragment_recycler" />
<fragment
android:id="@+id/tabFragment"
android:name="com.example.currencyapp.TabFragment"
android:label="TabFragment" >
<action
android:id="@+id/tab_to_main"
app:destination="@id/mainFragment" />
</fragment>
</navigation>
這些標題不是它們各自的布局 xml 的名稱,而是您在 nav_graph 中設置的android:label
nav_graph
。
如果您希望它們始終是應用程序的名稱,請設置android:label="@string/app_name"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.