GridView项彼此重叠

时间:2018-06-24 11:54:13

标签: java android gridview

请检查我所附的图像。产品名称超过一行时,我将面临此问题。我很困惑,现在该怎么办。

enter image description here

在我的gridview中,我面临着这个问题。我该如何解决?这是我的gridview xml代码。

<GridView
    android:id="@+id/productGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"
    android:verticalSpacing="4dp"
    android:horizontalSpacing="3dp"
    android:focusable="true"
    android:stretchMode="columnWidth"
    android:fitsSystemWindows="true">

</GridView>

这是我的网格项目xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.CardView
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="8dp"
        app:cardPreventCornerOverlap="true"
        app:contentPadding="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/iv_productImage"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:src="@drawable/avatar"/>
            <TextView
                android:id="@+id/tv_productName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:text="Bread Trimmer"
                android:textSize="17sp"
                android:textStyle="bold"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_gravity="center"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp">
                <ImageView
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:src="@drawable/coin"/>
                <TextView
                    android:id="@+id/tv_productPrice"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="1049999999999990"
                    android:layout_marginLeft="5dp"
                    android:layout_marginStart="5dp"
                    android:textColor="#FAAD18"
                    android:textSize="17sp"/>
            </LinearLayout>

            <TextView
                android:id="@+id/btn_buy"
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="12dp"
                android:paddingRight="12dp"
                android:layout_height="wrap_content"
                android:text="Get Now"
                android:textColor="#FFFFFF"
                android:focusable="false"
                android:textSize="16sp"
                android:gravity="center"
                android:background="@drawable/buy_background"
                android:textStyle="bold"
                android:textAllCaps="false"/>
        </LinearLayout>

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

</RelativeLayout>

这是单个项目。我很困惑我在哪里犯错了。请帮忙。谢谢。

2 个答案:

答案 0 :(得分:1)

解决方案1 ​​

您可以将此属性添加到您的tv_productName

    android:ellipsize="end"
    android:singleLine="true"
    android:maxLines="1"

此行将...添加到TextView文本的结尾

解决方案2

将此库用于您的tv_productName TextView

autofittextview

此库适合您的TextView大小,以防止重叠

解决方案3

这是一个不好的解决方案,但它可以工作,您可以为父级布局(RelativeLayout)添加边距

android:layout_margin="5dp"

答案 1 :(得分:-1)

我认为是问题所在。您的文本视图。产品名称。随着产品名称的增加,整个文本视图将被划分并垂直分布。像水钻玫瑰手表一样,手表处于与上面的文字相关的垂直位置,这在造成问题。我猜。尝试使用小文本并检查是否是问题所在,如果那是问题,您可以轻松解决它,我想,请让我知道它是否有效。谢谢 。