如何解决Android:使 TextView 宽度与水平 recyclerView 中的 ImageView 匹配
我有一个水平回收器视图,其中包含一个图像和一个标题。我需要 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。
解决方法
您可以使您的 TetxView 多行使用
android: singleline = "false"
然后使用“max lines”属性设置允许的最大行数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。