[英]Android: Have TextView width match ImageView in horizontal 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。
您可以使您的 TetxView 多行使用
android: singleline = "false"
然后使用“最大行數”屬性設置允許的最大行數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.