为什么最新版本的ConstraintLayout在带有CoordinatorLayout的nestedScrollView中不起作用 [英] Why latest version constraintLayout doesn't work in nestedScrollView with CoordinatorLayout
问题描述
我在滚动时使用了带有collapsingToolbar的coordinatorlayout来实现视差效果
i have using coordinatorlayout with collapsingToolbar for parallax effect when scroll
和collapsingToolbar中的内容视图(关于页面的文本)底部 nestedScrollView.
and content view (text about page) bottom of collapsingToolbar in nestedScrollView.
我的问题是nestedScrollView内的约束布局似乎不完整
my problem is constraint layout seems incomplete inside nestedScrollView
如果我在nestedScrollView内容视图中使用LinearLayout,则一切都将显示. 但是我在nestedScrollView内容视图中使用ConstraintLayout似乎不完整,因为您可以看到以下代码
if i use LinearLayout in nestedScrollView content view everything is showing. but i using ConstraintLayout in nestedScrollView content view is seems incomplete as you can see below code
使用约束布局制作的内容视图
content view made with constraint layout
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_anchor="@+id/appBarLayout"
app:layout_anchorGravity="bottom">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapsedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
app:contentScrim="@color/transparentBlack"
app:expandedTitleGravity="bottom"
app:expandedTitleMarginBottom="@dimen/input_fields_margin"
app:expandedTitleMarginTop="@dimen/shrine_toolbar_offset_top"
app:expandedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/newsPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/news_detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
android:theme="@style/ToolBarStyle.NewsDetails"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title=""
app:titleTextColor="@android:color/white" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:scrollbarSize="5dp"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="400dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/newsTitle"
style="@style/PrimaryTextFieldBold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/small_margin"
android:maxLines="3"
android:textSize="@dimen/primary_text_large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/newsDate"
style="@style/PrimaryTextFieldBold"
android:layout_width="wrap_content"
android:layout_margin="@dimen/small_margin"
android:textSize="@dimen/primary_text_medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/newsTitle"
tools:text="08.08.2008 18:00" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/photoOwner"
style="@style/PrimaryTextFieldBold"
android:layout_margin="@dimen/small_margin"
android:layout_weight="1"
android:textSize="@dimen/primary_text_medium"
app:layout_constraintBottom_toBottomOf="@id/newsDate"
app:layout_constraintStart_toEndOf="@id/newsDate"
app:layout_constraintTop_toTopOf="@id/newsDate"
tools:text="Daily News" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/showComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:padding="@dimen/small_margin"
android:src="@drawable/ic_comment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/newsTitle" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/newsText"
style="@style/PrimaryTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/small_margin"
android:paddingBottom="32dp"
android:textSize="@dimen/primary_text_medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/newsDate"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如果我已经使用Linearlayout,那就可以了.
if i have using Linearlayout it is work.
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_anchor="@+id/appBarLayout"
app:layout_anchorGravity="bottom">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapsedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
app:contentScrim="@color/transparentBlack"
app:expandedTitleGravity="bottom"
app:expandedTitleMarginBottom="@dimen/input_fields_margin"
app:expandedTitleMarginTop="@dimen/shrine_toolbar_offset_top"
app:expandedTitleTextAppearance="@style/PrimaryTextField.NewsDetails.Expanded"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/newsPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/news_detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
android:theme="@style/ToolBarStyle.NewsDetails"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title=""
app:titleTextColor="@android:color/white" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:scrollbarSize="5dp"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="400dp"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/newsTitle"
style="@style/PrimaryTextFieldBold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/small_margin"
android:maxLines="3"
android:textSize="@dimen/primary_text_large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/newsDate"
style="@style/PrimaryTextFieldBold"
android:layout_width="wrap_content"
android:layout_margin="@dimen/small_margin"
android:textSize="@dimen/primary_text_medium"
tools:text="08.08.2008 18:00" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/photoOwner"
style="@style/PrimaryTextFieldBold"
android:layout_margin="@dimen/small_margin"
android:layout_weight="1"
android:textSize="@dimen/primary_text_medium"
tools:text="Daily News" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/showComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:padding="@dimen/small_margin"
android:src="@drawable/ic_comment" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/newsText"
style="@style/PrimaryTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/small_margin"
android:paddingBottom="32dp"
android:textSize="@dimen/primary_text_medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/newsDate"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
约束布局版本"2.0.0-beta2"
Constraint layout version "2.0.0-beta2"
推荐答案
如果您使用约束"2.0.0-beta 2",那么我建议您在示例中尝试运动布局". Motion Layout是从Constraint Layout扩展的布局类.运动版式具有约束版式的所有功能.
If you are using Constraint"2.0.0-beta 2" then I would recommended you to try Motion Layout in your example. Motion Layout is a layout class that extends from Constraint Layout. Motion Layout has all the features of Constraint Layout.
您可以参考此链接来了解运动布局 https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part -1/
you can refer this link to understand motion layout https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-1/
这篇关于为什么最新版本的ConstraintLayout在带有CoordinatorLayout的nestedScrollView中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!