【问题标题】:Android recyclerview with images带有图像的 Android 回收站视图
【发布时间】:2021-02-11 08:23:23
【问题描述】:

我想制作一个recyclerview 来显示用户拍摄的一些图像。在每个项目的回收站视图中,我在左侧都有作者和日期,在center-right 中,我调整了图像的大小,例如使用200dp 宽度和100dp 高度,当用户点击它时,我会显示它是全尺寸的。 问题是我无法在recyclerview中设置图像项的widthheight。我尝试了所有的scaleType,但没有任何效果,图像大小调整不正确或太高或太低。

recyclerview的xml:

<androidx.recyclerview.widget.RecyclerView
               android:id="@+id/photoList"
               android:layout_width="350dp"
               android:layout_height="0dp"
               android:layout_marginStart="@dimen/MarginStart20"
               android:layout_marginTop="15dp"
               android:background="@drawable/reclycler_border"
               android:paddingStart="10dp"
               android:paddingEnd="10dp"
               app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
               app:layout_constraintHeight_default="wrap"
               app:layout_constraintHeight_max="300dp"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toBottomOf="@id/textView20"
               tools:listitem="@layout/photo_list_item" />

每个项目的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:paddingBottom="10dp">

   <TextView
       android:id="@+id/photoAuthorView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="5dp"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       tools:text="Mario Rossi" />

   <TextView
       android:id="@+id/photoDateView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="5dp"
       android:singleLine="false"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toBottomOf="@+id/photoAuthorView"
       tools:text="10/01/2021\n10:00:00" />

   <ImageView
       android:id="@+id/photoView"
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:layout_marginStart="60dp"
       android:scaleType="fitEnd"
       app:layout_constraintStart_toEndOf="@+id/photoAuthorView"
       app:layout_constraintStart_toEndOf="@+id/photoAuthorView"
       tools:srcCompat="@tools:sample/backgrounds/scenic" />

</androidx.constraintlayout.widget.ConstraintLayout>

this是现在的样子,你可以看到图片位置不好,我要在作者和日期的右边

【问题讨论】:

  • 我添加了现在的截图
  • @ʍѳђઽ૯ท 如果您看到图片大小调整不正确
  • 删除此应用:layout_constraintBottom_toBottomOf="@+id/photoDateView" 并添加到应用:layout_constraintTop_toTopOf="@id/photoAuthorView" 图像视图底部与日期视图底部对齐
  • @KishanMaurya 我做到了,但是现在如何解决对齐问题?我想要它在作者和日期的右边...请看我添加的新图片
  • android:scaleType="fitEnd" 尝试使用 fitCenter 或 fitxy

标签: android xml kotlin android-recyclerview


【解决方案1】:

删除这个

app:layout_constraintBottom_toBottomOf="@+id/photoDateView"

并添加到

app:layout_constraintTop_toTopOf="@id/photoAuthorView"

imageView 底部与日期视图的底部对齐。

另外,检查秤类型。试试 FitXY

android:scaleType="fitXY"

【讨论】:

    【解决方案2】:

    试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginStart="10dp">
    
        <TextView
            android:id="@+id/photoAuthorView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Mario Rossi" />
    
        <TextView
            android:id="@+id/photoDateView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:singleLine="false"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/photoAuthorView"
            tools:text="10/01/2021\n10:00:00" />
    
        <ImageView
            android:id="@+id/photoView"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:scaleType="fitXY"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/backgrounds/scenic" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    结果:

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多