文本在“回收者”视图项中显示在屏幕外

时间:2020-02-17 12:28:57

标签: android android-layout android-recyclerview

我在回收站视图中使用了item.xml,但是文本不显示在屏幕上。我试图依靠这个answer

您可以通过为每个textview设置match_parent的宽度和适当的layout_weight来避免所有这些情况。

但是我不确定它在哪里

width="0"

应该被实施。

我的item.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>

有一个图像,其右侧是标题和描述。标题和说明均不显示在屏幕上:

enter image description here

我的回收站视图显示在我的片段中(如果有的话):

<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"/>

4 个答案:

答案 0 :(得分:2)

问题是,您仅定义了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">

//编辑:match_parent,因为ConstraintsLayouts根本不支持宽度和高度标识符。

答案 1 :(得分:1)

也将结束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">

答案 2 :(得分:1)

您可以尝试更改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">

答案 3 :(得分:1)

您为Linearlayout设置了换行内容,因此它将内容宽度作为宽度(最大为屏幕宽度)。因此您已定义了固定宽度

.gitignore
相关问题