我正在尝试在this link上制作一个看起来像这样但只有一列的仪表板
我想使仪表板可滚动。
我想在下面的代码上添加一个ScrollView。我已经尝试了各种方法,但是它一直在扭曲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>
答案 0 :(得分:1)
您应该将ScrollView添加为Layout的根,并添加约束布局作为其子级。
我已经编辑了您的代码。请尝试我做了一些更改。它正在工作。
<?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>
答案 1 :(得分:0)
您不应在ScrollView中使用GridView,在文档中 提的也一样。
您可以在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>