Android AppBarLayout 下的 NestedScroll
Android NestedScroll under AppBarLayout
我正在尝试使用具有自定义形状的工具栏,并且我想在 AppBarLayout 下显示 NestedScroll 中的文本。
但是我遇到了一些问题,正如您在这张截图中看到的那样:
我怎样才能实现这个目标?
我的XML:
<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:background="@android:color/transparent"
android:animateLayoutChanges="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@null"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="94dp"
android:background="@drawable/background_header_asset"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ipsun" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
PS.: 工具栏背景是可绘制的形状,所以它是透明的
编辑:
我的身材是:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="320dp"
android:height="94dp"
android:viewportWidth="320"
android:viewportHeight="94">
<path
android:pathData="M320,73.153L81.283,92.257C64.54,93.42 54.316,94
50.612,94c-3.705,0 -8.826,-1.771 -15.363,-5.314L0,70.176V0h320v73.153z"
android:fillColor="#01874D"
android:fillType="evenOdd"/>
</vector>
- 你有没有试过改变这个:
android:background="@null"
进入这个 android:background="@android:color/transparent"?
它在某些情况下会有所作为。
- 你能展示一下你的身材 xml 吗?确定没有白底吗?
- 您为应用程序主样式选择了哪种颜色的工具栏?是白色的吗?我正在尝试减少可能的问题源的数量。
您可以通过将当前工具栏分成两部分来获得您想要的外观:
- 一个普通的矩形工具栏,照常工作
- 固定在工具栏底部的自定义形状。
首先,制作一个较小的矢量可绘制对象(30dp
高,仅代表原始形状的底部):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="320dp"
android:height="30dp"
android:viewportWidth="320"
android:viewportHeight="30">
<path
android:pathData="M320,9.153 L81.283,28.257C64.54,29.42 54.316,30 50.612,30 46.907,30 41.786,28.229 35.249,24.686L0,6.176V0h320z"
android:strokeWidth="0.999999"
android:fillColor="#01874d"
android:fillType="evenOdd"/>
</vector>
然后,只需使用固定高度的标准矩形工具栏(64dp
使其看起来像以前一样)。
最后,在你的 NestedScrollView
之后添加一个 FrameLayout
,并将其锚定在你的应用栏底部:
<FrameLayout
android:id="@+id/customShapeOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_header_asset"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom"
android:layout_gravity="bottom"/>
FrameLayout
只是作为自定义形状的容器。因为它是在 NestedScrollView
之后添加到父 CoordinatorLayout
的,所以它将覆盖该视图(根据需要)。
这是完整的布局xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns: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="@android:color/transparent"
android:animateLayoutChanges="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@null"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/colorPrimary"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/ipsun" />
</androidx.core.widget.NestedScrollView>
<FrameLayout
android:id="@+id/customShapeOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_header_asset"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom"
android:layout_gravity="bottom"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
生成的布局如下所示:
我的猜测是 CoordinatorLayout
将 NestedScrollView
置于您的 AppBarLayout
之下,因为 ScrollingViewBehavior
。而且它只在其边界内呈现
尝试将 android:clipToPadding="false" 添加到 NestedScrollView
你可以在没有 CoordinatorLayout 的情况下试试这个
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns: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">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="94dp"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ipsun"/>
</androidx.core.widget.NestedScrollView>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="94dp"
android:background="@drawable/background_header_asset"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
主要想到这里:
- NestedScrollView 下的工具栏 else 文本将在工具栏上(看起来像绿色区域透明)
android:clipToPadding="false"
android:paddingTop="94dp"
填充值必须等于收费栏高度
我正在尝试使用具有自定义形状的工具栏,并且我想在 AppBarLayout 下显示 NestedScroll 中的文本。
但是我遇到了一些问题,正如您在这张截图中看到的那样:
我怎样才能实现这个目标?
我的XML:
<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:background="@android:color/transparent"
android:animateLayoutChanges="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@null"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="94dp"
android:background="@drawable/background_header_asset"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ipsun" />
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
PS.: 工具栏背景是可绘制的形状,所以它是透明的
编辑:
我的身材是:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="320dp"
android:height="94dp"
android:viewportWidth="320"
android:viewportHeight="94">
<path
android:pathData="M320,73.153L81.283,92.257C64.54,93.42 54.316,94
50.612,94c-3.705,0 -8.826,-1.771 -15.363,-5.314L0,70.176V0h320v73.153z"
android:fillColor="#01874D"
android:fillType="evenOdd"/>
</vector>
- 你有没有试过改变这个: android:background="@null" 进入这个 android:background="@android:color/transparent"? 它在某些情况下会有所作为。
- 你能展示一下你的身材 xml 吗?确定没有白底吗?
- 您为应用程序主样式选择了哪种颜色的工具栏?是白色的吗?我正在尝试减少可能的问题源的数量。
您可以通过将当前工具栏分成两部分来获得您想要的外观:
- 一个普通的矩形工具栏,照常工作
- 固定在工具栏底部的自定义形状。
首先,制作一个较小的矢量可绘制对象(30dp
高,仅代表原始形状的底部):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="320dp"
android:height="30dp"
android:viewportWidth="320"
android:viewportHeight="30">
<path
android:pathData="M320,9.153 L81.283,28.257C64.54,29.42 54.316,30 50.612,30 46.907,30 41.786,28.229 35.249,24.686L0,6.176V0h320z"
android:strokeWidth="0.999999"
android:fillColor="#01874d"
android:fillType="evenOdd"/>
</vector>
然后,只需使用固定高度的标准矩形工具栏(64dp
使其看起来像以前一样)。
最后,在你的 NestedScrollView
之后添加一个 FrameLayout
,并将其锚定在你的应用栏底部:
<FrameLayout
android:id="@+id/customShapeOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_header_asset"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom"
android:layout_gravity="bottom"/>
FrameLayout
只是作为自定义形状的容器。因为它是在 NestedScrollView
之后添加到父 CoordinatorLayout
的,所以它将覆盖该视图(根据需要)。
这是完整的布局xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns: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="@android:color/transparent"
android:animateLayoutChanges="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@null"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/colorPrimary"
app:layout_collapseMode="pin"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/ipsun" />
</androidx.core.widget.NestedScrollView>
<FrameLayout
android:id="@+id/customShapeOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_header_asset"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom"
android:layout_gravity="bottom"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
生成的布局如下所示:
我的猜测是 CoordinatorLayout
将 NestedScrollView
置于您的 AppBarLayout
之下,因为 ScrollingViewBehavior
。而且它只在其边界内呈现
尝试将 android:clipToPadding="false" 添加到 NestedScrollView
你可以在没有 CoordinatorLayout 的情况下试试这个
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns: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">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="94dp"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ipsun"/>
</androidx.core.widget.NestedScrollView>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="94dp"
android:background="@drawable/background_header_asset"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
主要想到这里:
- NestedScrollView 下的工具栏 else 文本将在工具栏上(看起来像绿色区域透明)
android:clipToPadding="false"
android:paddingTop="94dp"
填充值必须等于收费栏高度