Android:使 TextView 宽度与水平 recyclerView 中的 ImageView 匹配

时间:2021-05-17 21:40:40

标签: java android android-recyclerview

我有一个水平回收器视图,其中包含一个图像和一个标题。我需要 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。

1 个答案:

答案 0 :(得分:0)

您可以使您的 TetxView 多行使用

android: singleline = "false"

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

相关问题