[英]Android Studio - 2 Toolbars in Navigation Activity
我创建了一个简单的导航抽屉活动,它创建了 2 个工具栏。 顶部的那个是不可编辑的,我猜它是由导航抽屉以某种方式自动创建的,因为它会根据片段更改其上的标签。
示例 1: 2 条可见
示例 2:打开的导航抽屉
约翰的例子:点击查看截图
如何删除不可编辑的?
活动_main.xml:
<?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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
activity_main_drawer.xml:
<?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_home"
android:icon="@drawable/ic_menu_camera"
android:title="@string/menu_home" />
<item
android:id="@+id/nav_faq"
android:icon="@drawable/ic_menu_gallery"
android:title="@string/menu_faq" />
<item
android:id="@+id/nav_statistics"
android:icon="@drawable/ic_menu_slideshow"
android:title="@string/menu_statistics" />
<item
android:id="@+id/nav_dentist"
android:icon="@drawable/ic_menu_manage"
android:title="@string/menu_dentist" />
</group>
<item android:title="Beliebiger Titel">
<menu>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_settings" />
<item
android:id="@+id/nav_impressum"
android:icon="@drawable/ic_menu_send"
android:title="@string/menu_impressum" />
</menu>
</item>
</menu>
app_bar_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorError"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorError"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="#00000000"
app:srcCompat="@android:drawable/ic_menu_add" />
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
通过删除 app_bar_main.xml 中的工具栏项目,我可以摆脱其中之一,但我不知道如何/在哪里编辑剩下的那个。 非常感谢您提前!
您可以通过编程方式自定义 app_bar_main 中的一个。 我不明白你为什么要创建另一个。 如果您使用导航库,标签会根据您的 nav_graph 标签而变化。
要编辑您的工具栏,您可以在您的片段中调用val actionBar = (activity as AppCompatActivity).supportActionBar
,然后使用它做任何您想做的事情。
可以编辑带有 NavigationView 的工具栏。
app_bar_main.xml:
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.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/AppTheme.PopupOverlay" >
<androidx.appcompat.widget.AppCompatImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_menu_send"/>
<SearchView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.appcompat.widget.Toolbar>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.