如何用 Toolbar 替换 AppBarLayout?
How to replace AppBarLayout with Toolbar?
也许听起来很奇怪,但情况是这样的:
在 activity_main 我有 AppBarLayout:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/top_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/page_title"
app:navigationIcon="@drawable/ic_baseline_menu_24"
app:menu="@menu/top_toolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"/>
</com.google.android.material.appbar.AppBarLayout>
和一个切换到另一个屏幕的按钮,fragment_details。在那里我有 CoordinatorLayout with Toolbar 和 NestedScrollView:
<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"
android:fitsSystemWindows="false"
tools:context=".DetailsFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
android:theme="@style/Theme.Loovie">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@id/details_toolbar">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/details_poster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:background="@color/teal_200"
android:src="@drawable/ic_launcher_foreground"
app:layout_collapseMode="parallax"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/details_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/Theme.Loovie" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/details_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="@tools:sample/lorem/random"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
现在,如果我向下滚动并且 CoordinatorLayout 折叠,工具栏会出现在 AppBarLayout 下方并且有 both of them on the screen. But I need only Toolbar to appear in the place of AppBarLayout. I need the following logic: in fragment_details when I scroll down, AppBarLayout must be replaced with Toolbar. 是否有关于如何实现此目的的任何提示?
我不建议您在片段中放置任何工具栏,因为片段通常不用于保存工具栏。您的片段基本上是一个迷你 activity 本身,不知道它 activity 的 UI 组件。根据加载的片段调整 Activity 的工具栏会更有意义。您可以为此查看 Android Navigation Component。
但是,我不确定您的用例,因为您想要折叠工具栏效果。如果您仍想在片段 CoordinatorLayout 折叠时隐藏 activity 工具栏,您可以查看提供的解决方案 here 并使用该解决方案以编程方式访问您的活动工具栏并更改其可见性。或者更确切地说,使用偏移来平滑 decrease/increase activity 的工具栏不透明度 属性。
也许听起来很奇怪,但情况是这样的:
在 activity_main 我有 AppBarLayout:
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/top_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/page_title"
app:navigationIcon="@drawable/ic_baseline_menu_24"
app:menu="@menu/top_toolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"/>
</com.google.android.material.appbar.AppBarLayout>
和一个切换到另一个屏幕的按钮,fragment_details。在那里我有 CoordinatorLayout with Toolbar 和 NestedScrollView:
<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"
android:fitsSystemWindows="false"
tools:context=".DetailsFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
android:theme="@style/Theme.Loovie">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@id/details_toolbar">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/details_poster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:background="@color/teal_200"
android:src="@drawable/ic_launcher_foreground"
app:layout_collapseMode="parallax"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/details_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/Theme.Loovie" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/details_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="@tools:sample/lorem/random"/>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
现在,如果我向下滚动并且 CoordinatorLayout 折叠,工具栏会出现在 AppBarLayout 下方并且有 both of them on the screen. But I need only Toolbar to appear in the place of AppBarLayout. I need the following logic: in fragment_details when I scroll down, AppBarLayout must be replaced with Toolbar. 是否有关于如何实现此目的的任何提示?
我不建议您在片段中放置任何工具栏,因为片段通常不用于保存工具栏。您的片段基本上是一个迷你 activity 本身,不知道它 activity 的 UI 组件。根据加载的片段调整 Activity 的工具栏会更有意义。您可以为此查看 Android Navigation Component。
但是,我不确定您的用例,因为您想要折叠工具栏效果。如果您仍想在片段 CoordinatorLayout 折叠时隐藏 activity 工具栏,您可以查看提供的解决方案 here 并使用该解决方案以编程方式访问您的活动工具栏并更改其可见性。或者更确切地说,使用偏移来平滑 decrease/increase activity 的工具栏不透明度 属性。