【问题标题】:Android: Have TextView width match ImageView in horizontal recyclerViewAndroid:在水平 recyclerView 中有 TextView 宽度匹配 ImageView
【发布时间】:2021-08-07 04:01:12
【问题描述】:

我有一个水平回收器视图,其中包含一个图像和一个标题。我需要 recyclerview 显示所有具有相同分隔和文本下方的图像。如果文本超出图像的宽度,则应换行或截断。但是,在我当前的设置中,长文本会导致图像之间出现巨大的间隙,因为它们保持在一行中。

这是回收站视图项的 xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    app:cardElevation="0dp">


        <FrameLayout
            android:id="@+id/media_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <ImageView
                android:id="@+id/topimage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:cropToPadding="true"
                android:paddingStart="8dp"
                android:paddingEnd="8dp"
                android:scaleType="fitXY" />



        <TextView
            android:id="@+id/title"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/topImage"
            android:padding="2dp"
            android:text="@string/placeholder_name"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:layout_marginTop="5dp"/>

        <TextView
            android:id="@+id/subtitle"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:padding="2dp"
            android:text="@string/placeholder_details"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:layout_marginBottom="10dp" />

            </RelativeLayout>
        </FrameLayout>


</androidx.cardview.widget.CardView>


</LinearLayout>

另外,在回收器视图适配器中,我将图像的宽度/高度设置为 5:7 的纵横比,如下所示:

    double widthDouble = (Resources.getSystem().getDisplayMetrics().widthPixels / 2) * 0.9;
    int width = (int) widthDouble;
    int height = (int) (width * 0.7);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
    viewHolder.topImage.setLayoutParams(lp);
    viewHolder.topImage.setScaleType(ImageView.ScaleType.FIT_CENTER);

如何更改此设置以使回收站视图项成为图像的宽度并将文本换行在图像下方?

PS:我正在使用 Java。

【问题讨论】:

    标签: java android android-recyclerview


    【解决方案1】:

    你可以让你的 TetxView 多行使用

    android: singleline = "false"
    
    

    然后使用“最大行数”属性来设置允许的最大行数。

    【讨论】:

    • 谢谢,但即使添加了这个,recyclerView 项也是全文的宽度而不是图像的宽度。我需要它是图像的宽度,并在需要时将文本换行。
    • 我可以看到您使用的是相对布局。您可以使用约束布局让您的生活更轻松。如果我是你,请在 TextView 中尝试使用:'layout_constraintEnd_toEndOf="yourImageViewIdHere" 和 'layout_constraintStart_toStartOf="yourImageViewIdHere"。 (更改为约束布局后)您可以在此处查看如何使用它:developer.android.com/training/constraint-layout