有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在制作一个Android应用程序,它有一个活动、多个片段和一个NavigationDrawer。现在所有的片段都有相同的工具栏,但是我希望每个片段都有一个不同的工具栏,我找不到最好的方法。我读了一些教程,但每个人都用不同的方式来做,我想做最好的解决方案 有人能告诉我做这件事的最佳方法吗(没有必要写代码,只有想法)


共 (1) 个答案

  1. # 1 楼答案

    我的意见是使用工具栏作为每个片段布局的一部分。这样,您就有可能根据需要对其进行定制。如果每个屏幕都有一些共同点,您可以创建一个自定义工具栏,并将其包含在每个布局中

    例如:

    <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>
    

    “后退”按钮工具栏可以根据您的喜好进行自定义。 大概是这样的:

    <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>