Не удается правильно вложить GridView в Cardview - PullRequest
0 голосов
/ 11 апреля 2020

Я использую RecyclerView, который отображает CardView. Этот CardView имеет 2 TextViews и один GridView. GridView отображает CardView (давайте назовем его inside_card) в качестве дочернего элемента. Теперь я не могу изменить высоту этой внутренней карты. Ниже дано то, чего я хочу достичь (Изображение A) и что я получаю (Изображение B). Reference Image

Ниже приведены мои макеты. Activity_main. xml содержит RecylerView


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.LinearLayoutCompat 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=".Activities.MainActivity">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </androidx.recyclerview.widget.RecyclerView>  

    </androidx.appcompat.widget.LinearLayoutCompat>

активность_карта. xml Содержит макет для рендеринга внутри RecyclerView


    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.LinearLayoutCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView


        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        app:cardBackgroundColor="#AAA"

        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="4dp"
        android:layout_margin="8dp"
        android:requiresFadingEdge="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content"
            >
            <TextView
                android:id="@+id/proffName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="ProfessorName"
                android:textAlignment="center"
                android:gravity="center_horizontal">
            </TextView>
        <TextView
            android:id="@+id/subjectCode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SemesterName"
            android:textAlignment="center"
            android:gravity="center_horizontal">
        </TextView>

            <GridView
                android:layout_gravity="center"
                android:id="@+id/unitsGrid"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_weight="1"
                android:layout_margin="6dp"
                android:horizontalSpacing="2dp"
                android:numColumns="2"></GridView>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
    </androidx.appcompat.widget.LinearLayoutCompat>

inside_card. xml содержит макет, который будет отображаться внутри GridView

    <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/inside_card"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="8dp"
        app:cardCornerRadius="6dp"
        app:cardElevation="6dp"
        app:cardBackgroundColor="#eeeeee"
        >
        <TextView
            android:textAlignment="center"
            android:layout_gravity="center"
            android:id="@+id/unitName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="1dp"
            android:textColor="@color/cardview_dark_background"
            android:textSize="18dp" />
        </androidx.cardview.widget.CardView> ```

1 Ответ

0 голосов
/ 11 апреля 2020

Изменение внутренней_карты. xml

-> Увеличить высоту просмотра карты с 45dp до 120dp, настроить в соответствии с требованием

-> Установить угол карты 0dp, как в дизайне О, высота карты отсутствует.

-> Даже я предлагаю, не используйте для этого вид карты, просто возьмите простой LienarLayout и установите для этого рисованную форму с обводкой. Потому что установка границы в представлении карты - это немного утомительная задача.

 <androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inside_card"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="8dp"
    app:cardCornerRadius="6dp"
    app:cardElevation="0dp"
    app:cardBackgroundColor="#eeeeee"
    >
    <TextView
        android:textAlignment="center"
        android:layout_gravity="center"
        android:id="@+id/unitName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="1dp"
        android:textColor="@color/cardview_dark_background"
        android:textSize="18dp" />
    </androidx.cardview.widget.CardView> `

В вашем GridView также установите вертикальный интервал

      <GridView
            android:layout_gravity="center"
            android:id="@+id/unitsGrid"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:layout_margin="6dp"
            android:horizontalSpacing="15dp"
            android:verticalSpacing="15dp"
            android:numColumns="2"></GridView>
...