【问题标题】:Android NestedScroll under AppBarLayoutAppBarLayout下的Android NestedScroll
【发布时间】:2021-02-18 20:46:45
【问题描述】:

我正在尝试使用具有自定义形状的工具栏,并且我想在 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 material-design android-coordinatorlayout


    【解决方案1】:

    您可以通过将当前工具栏分成两部分来获得所需的外观:

    1. 一个普通的矩形工具栏,像往常一样工作
    2. 固定在工具栏底部的自定义形状。

    首先,制作一个较小的可绘制矢量(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>
    

    生成的布局如下所示:

    【讨论】:

      【解决方案2】:
      1. 您是否尝试过更改此设置: 安卓:背景=“@null” 进入这个 android:background="@android:color/transparent"? 在某些情况下,它可能会有所作为。
      2. 你能显示你的形状的 xml 吗?确定没有白色背景?
      3. 您为应用程序主样式选择了哪种颜色的工具栏?是白色的吗?我正在尝试减少可能的问题来源。

      【讨论】:

      • 1 - 是的,2- 我已经用形状编辑了我的问题,3- 绿色
      【解决方案3】:

      我的猜测是CoordinatorLayoutNestedScrollView 放在你的AppBarLayout 下方,因为ScrollingViewBehavior。它只在其边界内呈现

      尝试将 android:clipToPadding="false" 添加到 NestedScrollView

      【讨论】:

        【解决方案4】:

        不用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 下的工具栏,否则文本将在工具栏上(看起来像绿色区域透明)
        • android:clipToPadding="false"
        • android:paddingTop="94dp" 填充值必须等于收费栏高度

        【讨论】:

        • 与此答案的重复问题已被删除,但我认为它必须在这里,也许它可以帮助某人
        猜你喜欢
        • 1970-01-01
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多