【问题标题】:Confuse about recyclerview and imageview sizes对 recyclerview 和 imageview 大小感到困惑
【发布时间】:2020-04-24 21:42:05
【问题描述】:

我目前正在使用 recyclerview 来显示由每行图像组成的菜单。 我已将图像创建为 1080 x 300,以便可以在没有 mutch 问题的情况下按比例缩小

我无法理解 RecyclerView 以及项目布局和图像视图使用什么宽度选项。 我需要图像视图的高度约为 150dp,并且要占据整个屏幕,就像在编辑器上一样

编辑 1:

现在行之间有一个巨大的空白

【问题讨论】:

    标签: android-studio kotlin android-recyclerview imageview


    【解决方案1】:

    将此代码用于您自己创建的布局资源文件中的ImageView,例如custom.xml,即res/layout/custom.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="wrap_content">
    
    <ImageView
    android:id="@+id/imgItem"
    android:layout_width="match_parent"
    android:layout_height="150dp" />
    </RelativeLayout>
    

    将此代码用于RecyclerView

    android:width="match_parent"
    android:height="match_parent"
    

    【讨论】:

    • 这样做:)。但是现在我在每一行之间有一个巨大的空白区域
    • 空格可能只是通过您的图片高度为rootLayout设置背景颜色来查看背景颜色是否未显示意味着您的图片高度是造成此差距的原因。还有一件事也发布您的代码,以便我们可以看到错误在哪里。
    【解决方案2】:

    尝试将图像视图的比例更改为中心裁剪或适合 XY 像这样:

    android:scaleType="centerCrop"

    【讨论】:

      【解决方案3】:

      像这样在默认活动中使用 RecyclerView

      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <androidx.recyclerview.widget.RecyclerView
              android:id="@+id/recyclerview"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:visibility="visible">
      
                  <ImageView
                      android:layout_width = "match_parent"
                      android:layout_height = "wrap_content"
                      android:src= "" />                            
      
          </androidx.recyclerview.widget.RecyclerView>
      
      
      </RelativeLayout>
      

      注意 ImageView 的高度是环绕内容。考虑到每个图像的高度相等,这将正常工作

      【讨论】:

        猜你喜欢
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2014-11-02
        • 1970-01-01
        相关资源
        最近更新 更多