【问题标题】:Android RecyclerView messing up layout_weightAndroid RecyclerView 搞砸了 layout_weight
【发布时间】:2016-06-12 13:02:54
【问题描述】:

我在DialogFragment 中有一个非常简单的RecyclerView 单行项目应该显示图像、名字和姓氏以及一些数字。它们在 layout_weight 的帮助下进行布局,以平均共享可用空间。

由于某种原因,有些项目似乎随机计算 layout_weight 错误。

请注意,在编辑器预览中,将有问题的项目滚动出视图后,问题已修复,布局恢复正常。

正如您在图片中看到的,在第一项(并不总是(只是)第一项)中,图片占用了太多空间。

我的布局如下;

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/dodger_blue"
    android:orientation="horizontal"
    android:padding="8dp">

    <ImageView
        android:id="@+id/iv_face"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical">

        <TextView
            android:id="@+id/et_first_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLargeInverse"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/et_surname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMediumInverse"/>

    </LinearLayout>

    <TextView
        android:id="@+id/et_id"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMediumInverse"/>

    <TextView
        android:id="@+id/et_place"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.8"
        android:gravity="center|end"
        android:textAppearance="?android:attr/textAppearanceLargeInverse"/>
</LinearLayout>

还有我的适配器; https://gist.github.com/StefanDeBruijn/f032eac6619ac1b8420e352b883ea4dd

【问题讨论】:

  • 您是否在您的Adapter 中的inflate() 调用中传递父ViewGroup
  • 是的,我的整个适配器都添加了要点

标签: android android-recyclerview android-layout-weight


【解决方案1】:

我怀疑这是因为在图像视图中设置了大图像。因为您已将图像的高度设置为 match_parent。为了保持纵横比,它也在宽度上扩展。

也许您应该尝试将此属性添加到您的 xml 文件中并修复图像的高度。

android:scaleType="fitXY" android:layout_height=100dp

如果这能解决您的问题,请告诉我。

【讨论】:

  • 虽然所有项目都使用相同的图像,但它是保存在可绘制对象中的图像。我试过 scaleType,但这并没有解决它,也没有解释为什么它只发生在几个项目上,直到它们被滚动到视线之外
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 2012-04-14
相关资源
最近更新 更多