在我的项目中,当我在滚动视图中放置约束布局时,不会滚动,约束布局是固定的。这是我的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
xmlns:williamchart="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/mdrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="right"
android:layoutDirection="rtl">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back">
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.77"
android:background="#12315C"
android:gravity="right"
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:gravity="right"
android:orientation="vertical"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/hammenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingTop="6dp"
android:src="@drawable/hammenu" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:foregroundGravity="center_vertical|center|center_horizontal"
android:gravity="center|center_horizontal|center_vertical"
android:text="بازارها"
android:textColor="@android:color/background_light"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<ScrollView
android:id="@+id/scroll"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:fillViewport="true"
android:isScrollContainer="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/app_bar"
williamchart:layout_constraintEnd_toEndOf="parent"
williamchart:layout_constraintStart_toStartOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:isScrollContainer="true"
android:nestedScrollingEnabled="true"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialcardview8"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
williamchart:cardBackgroundColor="@android:color/transparent"
williamchart:cardCornerRadius="32dp"
williamchart:layout_constraintHeight_percent="0.25"
williamchart:layout_constraintTop_toBottomOf="@+id/materialcardview6"
williamchart:layout_constraintWidth_percent="0.4">
<io.alterac.blurkit.BlurLayout xmlns:blurkit="http://schemas.android.com/apk/res-auto"
android:id="@+id/blurLayout7"
android:layout_width="match_parent"
android:layout_height="match_parent"
blurkit:blk_downscaleFactor="0.30"
blurkit:blk_fps="0" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView7"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:elevation="4dp"
williamchart:layout_constraintBottom_toBottomOf="parent"
williamchart:layout_constraintDimensionRatio="h,15:9"
williamchart:layout_constraintEnd_toEndOf="parent"
williamchart:layout_constraintStart_toStartOf="parent"
williamchart:layout_constraintTop_toTopOf="parent"
williamchart:srcCompat="@drawable/mypardis" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialcardview6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
williamchart:cardBackgroundColor="@android:color/transparent"
williamchart:cardCornerRadius="32dp"
williamchart:layout_constraintHeight_percent="0.25"
williamchart:layout_constraintTop_toBottomOf="@+id/materialcardview4"
williamchart:layout_constraintWidth_percent="0.4">
<io.alterac.blurkit.BlurLayout xmlns:blurkit="http://schemas.android.com/apk/res-auto"
android:id="@+id/blurLayout5"
android:layout_width="match_parent"
android:layout_height="match_parent"
blurkit:blk_downscaleFactor="0.30"
blurkit:blk_fps="0" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:elevation="4dp"
williamchart:layout_constraintBottom_toBottomOf="parent"
williamchart:layout_constraintDimensionRatio="h,15:9"
williamchart:layout_constraintEnd_toEndOf="parent"
williamchart:layout_constraintStart_toStartOf="parent"
williamchart:layout_constraintTop_toTopOf="parent"
williamchart:srcCompat="@drawable/mypardis" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialcardview4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
williamchart:cardBackgroundColor="@android:color/transparent"
williamchart:cardCornerRadius="32dp"
williamchart:layout_constraintHeight_percent="0.25"
williamchart:layout_constraintTop_toBottomOf="@+id/materialcardview2"
williamchart:layout_constraintWidth_percent="0.4">
<io.alterac.blurkit.BlurLayout xmlns:blurkit="http://schemas.android.com/apk/res-auto"
android:id="@+id/blurLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
blurkit:blk_downscaleFactor="0.30"
blurkit:blk_fps="0" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:elevation="4dp"
williamchart:layout_constraintBottom_toBottomOf="parent"
williamchart:layout_constraintDimensionRatio="h,15:9"
williamchart:layout_constraintEnd_toEndOf="parent"
williamchart:layout_constraintStart_toStartOf="parent"
williamchart:layout_constraintTop_toTopOf="parent"
williamchart:srcCompat="@drawable/mypardis" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialcardview2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
williamchart:cardBackgroundColor="@android:color/transparent"
williamchart:cardCornerRadius="32dp"
williamchart:layout_constraintHeight_percent="0.25"
williamchart:layout_constraintTop_toTopOf="parent"
williamchart:layout_constraintWidth_percent="0.4">
<io.alterac.blurkit.BlurLayout xmlns:blurkit="http://schemas.android.com/apk/res-auto"
android:id="@+id/blurLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
blurkit:blk_downscaleFactor="0.30"
blurkit:blk_fps="0" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:elevation="4dp"
williamchart:layout_constraintBottom_toBottomOf="parent"
williamchart:layout_constraintDimensionRatio="h,15:9"
williamchart:layout_constraintEnd_toEndOf="parent"
williamchart:layout_constraintStart_toStartOf="parent"
williamchart:layout_constraintTop_toTopOf="parent"
williamchart:srcCompat="@drawable/mypardis" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:cardBackgroundColor="@android:color/transparent"
app:cardCornerRadius="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.4">
<io.alterac.blurkit.BlurLayout xmlns:blurkit="http://schemas.android.com/apk/res-auto"
android:id="@+id/blurLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
blurkit:blk_downscaleFactor="0.30"
blurkit:blk_fps="0" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:elevation="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="h,15:9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
williamchart:srcCompat="@drawable/mypardis" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right" />
<!--app:headerLayout="@layout/navigation_header"
app:menu="@menu/drawermenu"-->
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
但当我将约束转换为线性时,效果很好。我搜索了很多主题,并在滚动视图中放入了android:fillViewport="true“,但是如果我把滚动视图放在根视图中,我的约束布局就不会滚动,这是可以的
发布于 2019-07-22 12:51:18
在viewport为true时使用NestedScrollView
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="700dp">
</android.support.constraint.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
https://stackoverflow.com/questions/57146130
复制