Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

scrollview - How to make a GridLayout with CardViews scrollable in Android without the cards ending up overstretched?

I have pretty much described the problem in the question itself. I want to make the view scrollable, but by doing so everything gets super stretched out, which I don't want. I assume that the problem lies within the layout_height attributes and I've tried setting different ones to different values (match_parent, wrap_content, 0dp, even random integer dp) but nothing seems to work. I also tried adding a bunch of CardViews and that shrinks them but it doesn't shrink the RelativeLayout where the title is. (I will attach screenshots so you guys can see what's going on). Here's how it looks now. How it looks when I scroll down. How it looked when I added a lot CardViews.

Thank you in advance

Here is the code. (It seems long because it's repetitive)

<ScrollView 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"
    tools:context=".HowAreYou">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="10"
        android:background="@drawable/background2">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.5">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:fontFamily="sans-serif-condensed-light"
                android:text="Симптоми"
                android:textAlignment="center"
                android:textColor="#d76b73"
                android:textSize="40sp"
                android:textStyle="normal" />
        </RelativeLayout>
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="7"
            android:columnCount="2"
            android:rowCount="4"
            android:columnOrderPreserved="false">
            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">
                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/checkbox_cough"
                        android:onClick="onCheckBoxClicked"/>
                    <ImageView
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/cough"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:fontFamily="sans-serif-condensed-light"
                        android:text="Кашлица"
                        android:textAlignment="center"
                        android:textColor="@android:color/background_dark"
                        android:textSize="20sp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">
                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/checkbox_headache"
                        android:onClick="onCheckBoxClicked"/>
                    <ImageView
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/headache"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:fontFamily="sans-serif-condensed-light"
                        android:text="Главоболка"
                        android:textAlignment="center"
                        android:textColor="@android:color/background_dark"
                        android:textSize="20sp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">
                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/checkbox_fever"
                        android:onClick="onCheckBoxClicked"/>
                    <ImageView
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/fever"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:fontFamily="sans-serif-condensed-light"
                        android:text="Покачена температура"
                        android:textAlignment="center"
                        android:textColor="@android:color/background_dark"
                        android:textSize="20sp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                app:cardElevation="8dp"
                app:cardCornerRadius="8dp">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:layout_margin="16dp"
                    android:orientation="vertical">
                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/checkbox_stuffynose"
                        android:onClick="onCheckBoxClicked"/>
                    <ImageView
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/stuffynose"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:fontFamily="sans-serif-condensed-light"
                        android:text="Затнат нос"
                        android:textAlignment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the cardview replace the layout_height from '0dp' to wrap_content

 <androidx.cardview.widget.CardView
          android:layout_width="0dp"
          android:layout_height="wrap_content"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...