如何删除Android中此recyclerview元素中的额外空间?

时间:2017-06-02 08:02:53

标签: android-layout android-recyclerview recycler-adapter

我在Android中实施了一个Recycler View来创建一个列表,但由于某种原因,我在两个元素之间看到了一个空格,而且我无法确定错误位置。至于为什么会发生这种情况,我的猜测是在行元素XML上。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview1="http://schemas.android.com/apk/res-auto"
android:id="@+id/row_top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:id="@+id/pos_cv"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:clickable="true"
    cardview1:cardCornerRadius="6dp"
    cardview1:cardElevation="4dp"
    cardview1:cardMaxElevation="6dp">

    <TextView
        android:id="@+id/manager1"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="15dp"
        android:text="Store Manager/Branch"
        android:textSize="14sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/retail"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:text="Abc Retail"
            android:textSize="10sp" />

        <View
            android:layout_width="170dp"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="25,000"
            android:textColor="@color/colorDarkPink"
            android:textSize="14sp"
            android:textStyle="bold" />

    </LinearLayout>

    <TextView
        android:id="@+id/address"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="50dp"
        android:text="Abc Retail"
        android:textSize="10sp" />

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

RecyclerView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
    android:id="@+id/list_view_header_ll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:weightSum="5"
    android:background="@color/colorDarkPink"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top 3 Performers"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryLight"
        android:layout_weight="3"
        android:textSize="16sp"
        android:lines="1"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sales"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryLight"
        android:layout_weight="2"
        android:textSize="16sp"
        android:lines="1"
        />

</LinearLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/performer_lv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/colorTransparent"
    android:layout_marginTop="16dp"
    android:dividerHeight="4dp"
    />

2 个答案:

答案 0 :(得分:1)

这是因为回收者的视野高度。尝试将recyclerview height更改为wrap_content.And在cardview布局中的线性布局使用高度wrap_content。

为您的cardview试用此代码:

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview1="http://schemas.android.com/apk/res-auto"
    android:id="@+id/row_top_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<android.support.v7.widget.CardView
    android:id="@+id/pos_cv"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:clickable="true" 
    android:layout_marginBottom="10dp"
    cardview1:cardCornerRadius="6dp"
    cardview1:cardElevation="4dp"
    cardview1:cardMaxElevation="6dp">

    <TextView
        android:id="@+id/manager1"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="15dp"
        android:text="Store Manager/Branch"
        android:textSize="14sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/retail"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:text="Abc Retail"
            android:textSize="10sp" />

        <View
            android:layout_width="170dp"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:text="25,000"
            android:textColor="@color/colorDarkPink"
            android:textSize="14sp"
            android:textStyle="bold" />

    </LinearLayout>

    <TextView
        android:id="@+id/address"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="50dp"
        android:text="Abc Retail"
        android:textSize="10sp" />

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

答案 1 :(得分:0)

尝试降低您的价值 机器人:layout_height =&#34; 50dp&#34; 如果它不起作用尝试减少其他视图中的值

相关问题