android - RecyclerView 中的 CardView 没有间隙和 textview 超出布局

标签 android android-layout android-recyclerview android-cardview

我有一个 CardView,我将其放入回收站 View 中。我制作的卡片 View 彼此之间没有任何间隙,即使我已经添加了边距和填充。文本也不在卡片 View 中。

这是我的代码的结果,文本在屏幕外,卡片 View 之间没有间隙:

enter image description here

这是列表 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>

这是回收器 View 布局:

<?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>

最佳答案

下面附加的代码将解决您的问题。希望它能帮助你。如果有用,请尝试批准。

如果您有任何其他问题,请在下面的评论部分中免费提问。我也会尽力解决这个问题。

<?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>

enter image description here

关于android - RecyclerView 中的 CardView 没有间隙和 textview 超出布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55380835/

相关文章:

android - 背景透明,前景较暗

android - 清除整个适配器并添加新元素时的 RecyclerView 动画

android - 在我的案例中,根据平台版本加载布局文件

android - 如何在 Android 中安全地存储访问 token 和 secret ?

Android ListView上的SQLite查询结果

android - 如何从 json 读取\uxxxx

android - 将文本保留在菱形背景中

android - 相同的 Ripple drawable(效果)在 ListView 和 RecyclerView 上的显示不同

android - RecyclerView 在插入时滚动

java - 在不创建新 Intent Android 的情况下将数据从一个 Activity 发送到另一个 Activity