简体   繁体   English

Android:导航抽屉中每个片段的不同工具栏

[英]Android: different toolbar for each fragment in navigation drawer

I'm making an Android app that have one activity and multiple fragments and a NavigationDrawer.我正在制作一个 Android 应用程序,它有一个活动和多个片段以及一个 NavigationDrawer。 Now all the fragments have the same toolbar, but i want a different toolbar for each fragment and i don't find the best method to do it.现在所有片段都有相同的工具栏,但我希望每个片段都有不同的工具栏,但我找不到最好的方法。 I read some tutorials but each person make it in a different way and I want to do the best solution Can someone explain me the best way to do this (it is no necessary to write the code, only the idea)我阅读了一些教程,但每个人都以不同的方式制作它,我想做最好的解决方案有人能解释一下最好的方法吗(不需要编写代码,只有想法)

My opinion is to use Toolbar as part of each fragment layout.我的意见是使用 Toolbar 作为每个片段布局的一部分。 That way you would have the possibility to customize it the way you need it.这样你就有可能按照你需要的方式定制它。 If there is something common about every screen you can make a custom toolbar and include it in every layout.如果每个屏幕都有共同点,您可以制作自定义工具栏并将其包含在每个布局中。

Example:例子:

<LinearLayout
        android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_gray"
        android:orientation="vertical">

        <include layout="@layout/back_button_toolbar" />
 
        <!-- Your layout here... -->
</LinearLayout>

And the back button toolbar may be customizable as you like.并且后退按钮工具栏可以根据需要进行自定义。 Something like this:像这样的东西:

<androidx.appcompat.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/back_toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/top_bar_height"
    android:background="@color/whitelabel_topbar_color"
    app:navigationIcon="@drawable/ic_navigate_back">

    <TextView
        android:id="@+id/toolbar_title"
        style="@style/ToolbarTitle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center" />

</androidx.appcompat.widget.Toolbar>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM