CardView в RecyclerView отсутствует пробел и textview отсутствует в макете

#android #android-layout #android-recyclerview #android-cardview

#Android #android-layout #android-recyclerview #android-cardview

Вопрос:

У меня есть CardView, который я помещаю в представление recycler. В представлении карты, которое я создаю, нет никаких пробелов друг между другом, хотя я уже добавляю поля и отступы. также текст отсутствует в представлении карты.

Это результат моего кода, текст отсутствует на экране, и между представлением карты нет разрыва:

введите описание изображения здесь

Это список xml :

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="5dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@ id/tv_sales_invoice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="invoice"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@ id/tv_sales_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="date"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/tv_sales_invoice" />

        <View
            android:id="@ id/top_line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="5dp"
            android:background="@color/colorBlack"
            app:layout_constraintTop_toBottomOf="@ id/tv_sales_date" />

        <TextView
            android:id="@ id/sales_itemname_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/item_name"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/top_line" />

        <TextView
            android:id="@ id/sales_itemqty_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/item_qty"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toRightOf="@ id/sales_itemname_header"
            app:layout_constraintRight_toRightOf="@ id/sales_itemprice_header"
            app:layout_constraintTop_toTopOf="@ id/sales_itemname_header" />

        <TextView
            android:id="@ id/sales_itemprice_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/item_price"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@ id/sales_itemname_header" />

        <View
            android:id="@ id/mid_line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorBlack"
            app:layout_constraintTop_toBottomOf="@ id/sales_itemname_header" />

        <TextView
            android:textAlignment="textStart"
            android:id="@ id/tv_sales_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item"
            android:textSize="18sp"
            app:layout_constraintLeft_toLeftOf="@ id/sales_itemname_header"
            app:layout_constraintRight_toRightOf="@ id/sales_itemname_header"
            app:layout_constraintTop_toBottomOf="@ id/sales_itemname_header" />

        <TextView
            android:id="@ id/tv_sales_qty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="qty"
            android:textSize="18sp"
            app:layout_constraintLeft_toLeftOf="@ id/sales_itemqty_header"
            app:layout_constraintRight_toRightOf="@ id/sales_itemqty_header"
            app:layout_constraintTop_toBottomOf="@ id/sales_itemqty_header" />

        <TextView
            android:textAlignment="viewEnd"
            android:id="@ id/tv_sales_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/price"
            android:textSize="18sp"
            app:layout_constraintLeft_toLeftOf="@ id/sales_itemprice_header"
            app:layout_constraintRight_toRightOf="@ id/sales_itemprice_header"
            app:layout_constraintTop_toBottomOf="@ id/sales_itemprice_header" />

    </android.support.constraint.ConstraintLayout>


</android.support.v7.widget.CardView>
  

это макет для просмотра в режиме рециркуляции:

 <?xml version="1.0" encoding="utf-8"?>
<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=".SalesHistoryActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@ id/tv_sales_history"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/sales_history"
            android:textSize="25dp"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@ id/sales_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:padding="10dp"
            android:nestedScrollingEnabled="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/tv_sales_history"></android.support.v7.widget.RecyclerView>


    </android.support.constraint.ConstraintLayout>
</ScrollView>
  

Ответ №1:

Приведенный ниже код решит вашу проблему. Надеюсь, это поможет вам. Пожалуйста, попробуйте одобрить это, если это полезно.

Если у вас есть какие-либо другие вопросы, не стесняйтесь задавать их в разделе комментариев ниже. Я сделаю все возможное, чтобы решить и это.

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@ id/cardView"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="5dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@ id/tv_sales_invoice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="invoice"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@ id/tv_sales_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="date"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/tv_sales_invoice" />

        <View
            android:id="@ id/top_line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="5dp"
            android:background="@color/colorBlack"
            app:layout_constraintTop_toBottomOf="@ id/tv_sales_date" />

        <TextView
            android:id="@ id/sales_itemname_header"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="item_name"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toStartOf="@ id/sales_itemqty_header"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/top_line" />

        <TextView
            android:id="@ id/sales_itemqty_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="item_qty"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toStartOf="@ id/sales_itemprice_header"
            app:layout_constraintLeft_toRightOf="@ id/sales_itemname_header"
            app:layout_constraintTop_toTopOf="@ id/sales_itemname_header" />

        <TextView
            android:id="@ id/sales_itemprice_header"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Item price"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toEndOf="@id/sales_itemqty_header"
            app:layout_constraintTop_toTopOf="@ id/sales_itemname_header" />

        <View
            android:id="@ id/mid_line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorBlack"
            app:layout_constraintTop_toBottomOf="@ id/sales_itemname_header" />

        <TextView
            android:id="@ id/tv_sales_type"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Lorem Ipsum is simply dummy text"
            android:textAlignment="textStart"
            android:textSize="18sp"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="@ id/sales_itemname_header"
            app:layout_constraintRight_toRightOf="@ id/sales_itemname_header"
            app:layout_constraintTop_toBottomOf="@ id/mid_line" />

        <TextView
            android:id="@ id/tv_sales_qty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="qty"
            android:textSize="18sp"
            app:layout_constraintLeft_toLeftOf="@ id/sales_itemqty_header"
            app:layout_constraintRight_toRightOf="@ id/sales_itemqty_header"
            app:layout_constraintTop_toBottomOf="@ id/sales_itemqty_header" />

        <TextView
            android:id="@ id/tv_sales_price"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:text="Rp 13000.00"
            android:textSize="18sp"
            app:layout_constraintLeft_toLeftOf="@ id/sales_itemprice_header"
            app:layout_constraintRight_toRightOf="@ id/sales_itemprice_header"
            app:layout_constraintTop_toBottomOf="@ id/sales_itemprice_header" />

    </android.support.constraint.ConstraintLayout>


</android.support.v7.widget.CardView>
  

введите описание изображения здесь

Комментарии:

1. но в представлении card по-прежнему нет пробелов друг между другом. вы знаете, почему?

Ответ №2:

Вы должны использовать wrap_content в своей карточке.

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res       /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
 android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp">
 ....