【问题标题】:Incorrect xml format on recycle view page回收视图页面上的 xml 格式不正确
【发布时间】:2016-10-28 01:19:55
【问题描述】:

我正在开发一个 android 应用程序,在该应用程序中我使用来自我的网络服务器的项目填充我的回收视图。

问题, 我不太清楚我的 recyclerview 出了什么问题,每个“项目”似乎占据了整个屏幕,我查看了其他 XML 格式,它们似乎与我的相同。然而我有很大的余地。 参考图片

XML

自定义行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="16dp"
    >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cv"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_photo"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="16dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_name"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_alignParentTop="true"
                android:textSize="30sp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/person_age"
                android:layout_toRightOf="@+id/person_photo"
                android:layout_below="@+id/person_name"
                />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

回收器碎片

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/cointainerDrawerID"
    android:layout_height="match_parent">






        <android.support.v7.widget.RecyclerView
            android:id="@+id/drawrList"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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




</RelativeLayout>

在图片中,您会看到两个项目,但一个位于另一个下方。它位于完全不同的页面上。

【问题讨论】:

  • 你必须给CardView一个固定的高度

标签: android xml android-layout


【解决方案1】:

在您的代码中,您的商品有 android:layout_height="match_parent" 将其更改为wrap_content 或某个固定值将解决您的问题

【讨论】:

    【解决方案2】:

    将父线性布局的android:layout_height="match_parent"改为android:layout_height="wrap_content"

    使用卡片视图作为父布局。

    【讨论】:

      【解决方案3】:

      问题在于“layout_height”属性。将其更改为“wrap_content”或简单地将以下代码粘贴到您的 item.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="wrap_content"
        android:padding="16dp"
      >
      
      <android.support.v7.widget.CardView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/cv"
          >
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="16dp"
              >
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/person_photo"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentTop="true"
                  android:layout_marginRight="16dp"
                  />
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/person_name"
                  android:layout_toRightOf="@+id/person_photo"
                  android:layout_alignParentTop="true"
                  android:textSize="30sp"
                  />
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/person_age"
                  android:layout_toRightOf="@+id/person_photo"
                  android:layout_below="@+id/person_name"
                  />
          </RelativeLayout>
      </android.support.v7.widget.CardView>
      

      【讨论】:

        【解决方案4】:

        Recyclerview 有一个 LayoutManager,你可以这样使用它:

        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recycler.setHasFixedSize(true);
        

        【讨论】:

          猜你喜欢
          • 2020-09-24
          • 1970-01-01
          • 1970-01-01
          • 2019-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-26
          相关资源
          最近更新 更多