简体   繁体   English

Android:在水平回收器视图中具有 TextView 宽度匹配 ImageView

[英]Android: Have TextView width match ImageView in horizontal recyclerView

I have a horizontal recycler view which contains an Image and below that a title.我有一个水平回收器视图,其中包含一个图像和一个标题。 I need for the recyclerview to display all the images with the same separation and below the text.我需要 recyclerview 显示所有具有相同分隔和文本下方的图像。 If the text exceeds the width of the image it should either wrap or be cut off.如果文本超出图像的宽度,则应换行或截断。 However, with my current setup long texts are causing huge gaps between the images because they remain in a single line.但是,在我当前的设置中,长文本会导致图像之间出现巨大的间隙,因为它们保持在一行中。

This is the xml for the recycler view item:这是回收站视图项目的 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>

Also, within the recycler view adapter I'm setting the width/height of the image to a 5:7 aspect ratio like this:此外,在回收器视图适配器中,我将图像的宽度/高度设置为 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);

How can I change this in order to have recycler view item be the width of the Image and have the text wrap under the image?如何更改此设置以使回收站视图项成为图像的宽度并将文本换行在图像下?

PS: I'm using Java. PS:我正在使用 Java。

You can make your TetxView multi-line using您可以使您的 TetxView 多行使用

android: singleline = "false"

and then use the "max lines" property to set the max lines permitted.然后使用“最大行数”属性设置允许的最大行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM