【问题标题】:Android: different toolbar for each fragment in navigation drawerAndroid:导航抽屉中每个片段的不同工具栏
【发布时间】:2021-02-04 15:30:27
【问题描述】:

我正在制作一个具有一个活动和多个片段以及一个 NavigationDrawer 的 Android 应用。现在所有片段都有相同的工具栏,但我希望每个片段都有一个不同的工具栏,但我找不到最好的方法。我阅读了一些教程,但每个人都以不同的方式制作它,我想做最好的解决方案 有人能解释一下最好的方法吗(不需要写代码,只有想法)

【问题讨论】:

  • 我为每个片段做一个工具栏,效果很好。共享工具栏充其量是一件令人头疼的事情,根据我的经验,很少有这样做的理由。
  • @Nicolas 所以我为每个工具栏制作了 xml 并导入到片段中?以及导航抽屉的默认工具栏是如何消失的?
  • @AceStan 这个例子?
  • 对于导航抽屉,您可以将其设为活动的公共字段,然后在片段中使用((YourActivity) getActivity()).navigationDrawer 获取它,您可以对其进行设置,以便它可以打开导航抽屉并显示“汉堡”图标。
  • @Nicolas 如果你共享一个工具栏,你会得到一个可爱的汉堡包变成一个返回箭头的动画。 :)

标签: java android xml kotlin


【解决方案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>

【讨论】:

  • 这是一个片段xml的例子,不是吗?
  • 抱歉我的问题,但我是 android 开发的新手......导航抽屉有一个默认工具栏,可以放入每个片段,或者没有?如果是,我该如何删除它以放置我的工具栏
猜你喜欢
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 2019-09-15
相关资源
最近更新 更多