簡體   English   中英

文本在回收站視圖項中超出屏幕

[英]Text goes out of screen in recycler view item

我在回收器視圖中使用了 item.xml,但文本超出了屏幕。 我試圖依靠這個答案

您可以通過為每個textview提供 match_parent 的寬度和適當的 layout_weight 來避免所有這些。

但我覺得很困惑,因為我不確定在哪里

width="0"

應該實施。

我的項目.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/productLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="@dimen/product_recycler_view_margin">

    <ImageView
        android:id="@+id/photoIV"
        android:layout_width="@dimen/product_image_size"
        android:layout_height="@dimen/product_image_size"
        android:scaleType="centerCrop"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/product_image_size"
        android:orientation="vertical"
        android:gravity="top"
        android:layout_marginStart="@dimen/product_details_margin_left"
        app:layout_constraintStart_toEndOf="@+id/photoIV"
        app:layout_constraintTop_toTopOf="@id/photoIV">

        <TextView
            android:id="@+id/titleTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Top"

            android:textStyle="bold"
            android:textSize="@dimen/product_details_title" />

        <TextView
            android:id="@+id/descriptionTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/product_details_margin_middle"
            android:text="Bottom"
            android:textSize="@dimen/product_details_description_text_size" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

有一個圖像,其右側有標題和描述。 標題和描述都從屏幕上消失:

在此處輸入圖片說明

我的片段中的回收者視圖顯示它(如果重要的話):

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/productsRV"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

問題是,您只定義了 LinearLayout 的左約束,但沒有定義右約束。

所以你需要添加正確的約束。 此外,您需要用 0dp 替換此處的寬度,因為 0 在此上下文中表示“匹配約束”。

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="@dimen/product_image_size"
    android:orientation="vertical"
    android:gravity="top"
    android:layout_marginStart="@dimen/product_details_margin_left"
    app:layout_constraintStart_toEndOf="@+id/photoIV"
    app:layout_constraintTop_toTopOf="@id/photoIV"
    app:layout_constraintEnd_toEndOf="parent">

// EDIT: match_parent因為 ConstraintsLayouts 根本不支持寬度和高度標識符。

也將 End Constraint分配給您的LinearLayout

  <LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:gravity="top"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/photoIV"
    app:layout_constraintTop_toTopOf="@id/photoIV">

你可以嘗試改變 LinearLayout 像

<LinearLayout
        android:layout_width="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_height="@dimen/product_image_size"
        android:orientation="vertical"
        android:gravity="top"
        android:layout_marginStart="@dimen/product_details_margin_left"
        app:layout_constraintStart_toEndOf="@+id/photoIV"
        app:layout_constraintTop_toTopOf="@id/photoIV">

您為 Linearlayout 設置了換行內容,因此它將內容寬度作為寬度(最高屏幕寬度)。 所以你已經定義了固定寬度

android:layout_width="0dp"
app:layout_constraintEnd_toEndOf="parent"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM