我在我的项目中使用了折叠工具栏布局,其中我使用了一个NestedScrollView
,其中我有多个卡片视图,其中内容不是静态的,而是在onActivityResult
中更改。我将CollapsingToolbarLayout
的最小高度设置为100dp。所以我发现其中有几个问题:
CollapsingToolbarLayout
的最小高度,这可以通过某种方式工作,但是如果我得到大的动态文本,它将消失NestedScrollView
的滚动能力,这是不希望的结果。我的layout.xml文件是:-
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:id="@+id/topBlock"
app:layout_constraintTop_toTopOf="parent"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/navigation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
app:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@null"
android:minHeight="100dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapse_bar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@android:color/holo_blue_light"
android:minHeight="100dp"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:layout_scrollInterpolator="@android:anim/decelerate_interpolator">
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="8dp"
app:backgroundTint="@color/white"
app:fabSize="normal"
app:layout_anchor="@+id/nestedScrollView"
app:layout_anchorGravity="top|center_horizontal"
app:srcCompat="@drawable/ic_launcher_background" />
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="wrap_content">
////My Scrolling Content(Contains many edit texts so its size may vary)
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我在MyActivity.kt文件中没有做任何事情。请帮我解决这个问题。从过去15天开始,我已经尝试了该社区中的几乎所有解决方案,但仍然没有运气。谢谢!
编辑:根据建议,我已将NestedScrollView
作为直接子对象更新了问题,并发布了Android Studio预览的屏幕截图,该屏幕显示高度已超过预览。它应该根据内容占用高度。如果我进行NestedScrollView
wrap_content,那么它的底部仍然留有空白。在后台,我正在运行“摄像头”,因此必须使其与父摄像头匹配,以使其覆盖全高。
答案 0 :(得分:1)
首先,由于您拥有ConstraintLayout
,因此不需要CoordinatorLayout
。同样,正如用户在评论中提到的,NestedScrollView
应该是CoordinatorLayout
的直接子元素,而不是ConstraintLayout
中的子元素。通过这种方式设计,某些行为可能无法按预期工作,并且实际上是错误的设计。
我进行了更改,这是您在预览中看到的内容:
更新后的布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/topBlock"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapse_bar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@android:color/holo_blue_light"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:layout_scrollInterpolator="@android:anim/decelerate_interpolator">
<androidx.appcompat.widget.Toolbar
android:id="@+id/myToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme"
app:title="Your title" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="MyRandomText"
android:textColor="@android:color/black"
android:textSize="42sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintCircleRadius="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20sp"
android:background="@android:color/holo_blue_light"
app:layout_constraintTop_toBottomOf="@id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="8dp"
android:src="@mipmap/ic_launcher"
app:backgroundTint="@color/white"
app:fabSize="normal"
app:layout_anchor="@+id/nestedScrollView"
app:layout_anchorGravity="top|center_horizontal" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
PS:请记住,NestedScrollView
应该是第一个孩子,只有一个孩子。
通过这种正确的方法,您将不必使用kotlin-java代码来实现滚动或其他行为,就可以使用标志和属性(例如{{1})来实现行为}和layout_scrollFlags
等。
答案 1 :(得分:0)
对于您的问题,我没有特别的解决方案,但我强烈建议您采用运动版式。我也在开发带有“折叠工具栏”的应用,并且我想像Spotify一样做视差效果。使用Coordinator布局和折叠工具栏真的很困难,除了有关它的文档不是很广泛之外,老实说我不知道自己在做什么。
我在线研究了其他实现方法,最后我发现了这一点。通过“运动版式”,您可以每周动画一次到非常小的细节。我知道将整个布局从CoordinatorLayout更改为MotionLayout的想法似乎很繁重,但事实并非如此。鉴于Motion Layout是从Constraint Layout扩展而来的,并且可以对布局的每个视图进行动画处理(如果您的父级是Motion Layout),那么如果有什么值得一提的话。 pro-ML的另一个论据是您拥有动画侦听器:例如,完成过渡后,会自动调用一个方法,这样您就可以根据需要链接动画,这真的很有趣。
签出一些可以使用Motion Layout构建的可能的动画。这些带有布局xml和动画xml,因此您可以从中学习(很棒的官方文档btw): https://developer.android.com/training/constraint-layout/motionlayout/examples
这是我用来模仿协调器布局内折叠工具栏行为的教程: https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-1/ https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-2/ (我个人只是经历了第1部分,然后自己继续)