使用 GridLayout 在 ConstraintLayout 中添加 ScrollView 它不起作用
Added ScrollView In Constaint Layout With GridLayout Its Not Working
我正在尝试在 this link 上制作一个看起来像这样的仪表板,但只有一列
我想让仪表板可以滚动。
我想在下面添加一个 ScrollView code.I 已经尝试了各种方法,但它总是扭曲 GridLayout。
我尝试在 GriLayout 上方添加 ScrollView。它完全扭曲了 GridLayout。
这是代码
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.Dashboard">
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/HeaderBar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme"
app:theme="@style/AppTheme"
tools:targetApi="lollipop" />
<View
android:id="@+id/bg_top_header"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_marginTop="50dp"
android:background="@drawable/ic_bg_topheader"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:nestedScrollingEnabled="true"
android:padding="10dp"
android:rowCount="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/bg_top_header"
tools:targetApi="lollipop">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="HouseHold Register"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 513"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Service Delivery Logbook"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 514"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<!----Other Views----->
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You should not use GridView inside a ScrollView, In the documentation
also its mention the same.
您可以在 ScrollView 中使用垂直方向的 LinearLayout,并将所有项目放入该布局中。
根据你的需要我正在更新你的代码,你可以用这个替换你的代码
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.Dashboard">
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/HeaderBar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme"
app:theme="@style/AppTheme"
tools:targetApi="lollipop" />
<View
android:id="@+id/bg_top_header"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_marginTop="50dp"
android:background="@drawable/ic_bg_topheader"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/bg_top_header"
tools:targetApi="lollipop">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="HouseHold Register"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 513"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Service Delivery Logbook"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 514"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<!----Other Views----->
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
您应该将 ScrollView 添加为布局的根,并将约束布局添加为其子布局。
我已经编辑了你的代码。请尝试我做了一些改变。它正在工作。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".activity.Dashboard"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/HeaderBar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme"
app:theme="@style/AppTheme"
tools:targetApi="lollipop"
/>
<View
android:id="@+id/bg_top_header"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_marginTop="50dp"
android:background="@drawable/ic_bg_topheader"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:nestedScrollingEnabled="true"
android:padding="10dp"
android:rowCount="5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/bg_top_header"
tools:targetApi="lollipop"
>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="HouseHold Register"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 513"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Service Delivery Logbook"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 514"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
我正在尝试在 this link 上制作一个看起来像这样的仪表板,但只有一列
我想让仪表板可以滚动。
我想在下面添加一个 ScrollView code.I 已经尝试了各种方法,但它总是扭曲 GridLayout。
我尝试在 GriLayout 上方添加 ScrollView。它完全扭曲了 GridLayout。
这是代码
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.Dashboard">
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/HeaderBar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme"
app:theme="@style/AppTheme"
tools:targetApi="lollipop" />
<View
android:id="@+id/bg_top_header"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_marginTop="50dp"
android:background="@drawable/ic_bg_topheader"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:nestedScrollingEnabled="true"
android:padding="10dp"
android:rowCount="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/bg_top_header"
tools:targetApi="lollipop">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="HouseHold Register"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 513"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Service Delivery Logbook"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 514"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<!----Other Views----->
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You should not use GridView inside a ScrollView, In the documentation also its mention the same.
您可以在 ScrollView 中使用垂直方向的 LinearLayout,并将所有项目放入该布局中。
根据你的需要我正在更新你的代码,你可以用这个替换你的代码
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.Dashboard">
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/HeaderBar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme"
app:theme="@style/AppTheme"
tools:targetApi="lollipop" />
<View
android:id="@+id/bg_top_header"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_marginTop="50dp"
android:background="@drawable/ic_bg_topheader"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/bg_top_header"
tools:targetApi="lollipop">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="HouseHold Register"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 513"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Service Delivery Logbook"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 514"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<!----Other Views----->
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
您应该将 ScrollView 添加为布局的根,并将约束布局添加为其子布局。
我已经编辑了你的代码。请尝试我做了一些改变。它正在工作。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".activity.Dashboard"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
style="@style/HeaderBar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme"
app:theme="@style/AppTheme"
tools:targetApi="lollipop"
/>
<View
android:id="@+id/bg_top_header"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_marginTop="50dp"
android:background="@drawable/ic_bg_topheader"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:nestedScrollingEnabled="true"
android:padding="10dp"
android:rowCount="5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/bg_top_header"
tools:targetApi="lollipop"
>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="HouseHold Register"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 513"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Service Delivery Logbook"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="MOH 514"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:background="@color/whiteColor"
app:cardCornerRadius="8dp"
app:cardElevation="6dp"
tools:targetApi="lollipop"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@mipmap/rgil_glasses"
android:gravity="start"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Diarrhoea Baseline"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:fontFamily="@font/mo_re"
android:text="Survey"
android:textColor="@color/colorBlack"
android:textSize="30sp"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>