【问题标题】:CardView layout has huge spacing inbetween them when i scroll down当我向下滚动时,CardView 布局之间的间距很大
【发布时间】:2021-06-07 11:03:54
【问题描述】:

我正在制作一种库存搜索应用程序。我有一个问题,我尝试在突然出现的卡片视图之间上下滚动一个巨大的空间。当我进行搜索时,布局会重置为正常,但是当我进行滚动时,问题就出现了。下面是我的 list_layout.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="6dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="1dp"
        app:cardCornerRadius="17dp"
        android:background="#EFEFEF"
        app:cardElevation="1dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:stretchColumns="0,1"
                android:layout_marginTop="20dp">

                <TableRow
                    android:layout_width="match_parent"
                    android:paddingBottom="8dp"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="170dp"
                            android:layout_height="150dp"
                            android:layout_column="0"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/textViewName"
                            android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:textColor="#000000"
                            android:textSize="15dp"
                            android:textStyle="bold">

                        </TextView>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:text="Over 700 items in store!"
                            android:textColor="#000000"
                            android:textSize="12dp"
                            android:textStyle="bold">

                        </TextView>

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="#FF0000">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="ON SALE !"
                                android:textColor="#fff"
                                android:textStyle="bold">

                            </TextView>

                        </LinearLayout>

                        <Button
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginTop="19dp"
                            android:background="@drawable/checkout_button"
                            android:onClick="groceries_click"
                            android:text="shop now!"
                            android:textAllCaps="false"
                            android:textSize="12dp">
                        </Button>
                    </LinearLayout>
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

The image of the view get before I scroll down. || This is the behaviour I get after a scroll

【问题讨论】:

    标签: android xml listview search android-linearlayout


    【解决方案1】:

    这是因为您在标签&lt;LinearLayout/&gt; 上添加了match_parent

    改成wrap_content

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" //Change into wrap_content
        android:padding="6dp"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">
    

    【讨论】:

      猜你喜欢
      • 2019-01-01
      • 2016-06-19
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多