约束布局中的TextView未显示

4
我正在创建一个简单的回收视图项目布局,其中包含一个图像和一个放置在约束布局内的文本视图。我正在将图像的约束设置为位于左侧,文本从图像的右侧开始,并延伸到父元素的末端。然而,当我这样做时,文本视图没有显示在屏幕上。请问有人能帮帮我吗?先谢谢了。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_character_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"
        android:minHeight="100dp"
        android:paddingBottom="20dp"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        android:paddingTop="20dp"
        android:textSize="24sp"
        android:textStyle="bold"
        tools:text="Homer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/img_character_photo"/>

    <ImageView
        android:id="@+id/img_character_photo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@color/colorAccent"
        android:padding="5dp"
        app:layout_constraintStart_toStartOf="parent"
        />

</android.support.constraint.ConstraintLayout>
</layout>

在此输入图片描述

如果我从textView中删除app:layout_constraintEnd_toEndOf="parent",则会得到以下结果。 在此输入图片描述


请检查文本颜色 - 我猜它是白色的(在我的示例应用程序中,您的布局看起来很好)。顺便说一句,如果您的视图未能如预期显示,LayoutInspector 是一个非常好的工具。 - Bö macht Blau
@0X0nosugar,当我删除app:layout_constraintEnd_toEndOf =“parent”约束时,文本出现在屏幕上,但右侧文本超出了边界。 - Abhi
3个回答

2

当使用ConstraintLayout时,建议您至少连接每个视图中轴线上的一个点,即应该垂直和水平连接至少一个点以使其正确。此外,如果您阅读有关如何正确使用ConstraintLayout的文档,它也会告诉您同样的事情。我相信这将解决您的问题(并且您还将在实际设备上看到所有视图)。

做类似于这样的事情:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_character_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"
        android:minHeight="100dp"
        android:paddingBottom="20dp"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        android:paddingTop="20dp"
        android:textSize="24sp"
        android:textStyle="bold"
        tools:text="Homer Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum
        Lorem Ipsum Lorem Ipsum"

        app:layout_constraintStart_toEndOf="@id/img_character_photo"
        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintEnd_toEndOf="parent" />

    <ImageView
        android:id="@+id/img_character_photo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@color/colorAccent"
        android:padding="5dp"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

上面代码的屏幕截图:

针对屏幕尺寸- 7.0英寸(1200 x 1920像素) [设备:Nexus 7]

输入图像描述

了解更多信息,请参见: https://codelabs.developers.google.com/codelabs/constraint-layout/index.html#7

希望这能帮到您。


如果您不介意的话,能否给个例子@Nitin。谢谢! - Abhi
是的,请尝试将ImageView的起点连接到父级的起点,将其顶部连接到父级的顶部,并将TextView的起点连接到ImageView的末端+将其顶部连接到父级的顶部,将其末端连接到父级的末端(因为这是您所需的条件之一)。其余所有点都是基本连接点,以便在实际设备上运行时所有视图都出现。 - Nitin Gurbani
添加了这5个约束条件。我仍然得到相同的结果。 - Abhi
请问一下您的ConstraintLayout依赖版本是多少? - Nitin Gurbani
它正在使用最新版本1.1.3。 - Abhi
显示剩余7条评论

1
尝试移除


android:maxLines="2" 

textView元素的属性

布局代码:

<android.support.constraint.ConstraintLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
    android:id="@+id/tv_character_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:minHeight="100dp"
    android:paddingBottom="20dp"
    android:paddingEnd="10dp"
    android:paddingStart="10dp"
    android:paddingTop="20dp"
    android:textSize="24sp"
    android:textStyle="bold"
    android:text="Homer Test SHOW Homer Test SHOW Homer TestSHOWHomer Test SHOW Homer Test SHOW Homer Test SHOW END"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/img_character_photo"/>

<ImageView
    android:id="@+id/img_character_photo"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@color/colorAccent"
    android:padding="5dp"
    app:layout_constraintStart_toStartOf="parent"
    />


确定吗?在我的情况下它是有效的... 它将行数限制为2,由于有更多的文本,因此它会显示在屏幕外... 从textview元素中删除此属性应该可以在更多行上在屏幕上显示所有文本。 - Dimness
是的,它使得条目视图变得更大了,但没有显示任何文本。我尝试从textView中删除parentEnd约束。这应该显示所有的文本,但仍然会从右侧越界。如果您不介意,请发布您的代码。谢谢。 - Abhi
尝试将:tools:text="Homer" 替换为:android:text="Homer" …… 如何在TextView中设置文本? 请注意,翻译仅供参考,具体情况还需根据上下文确定最合适的翻译。 - Dimness
文本设置在Recycler View适配器类中。tools:text只是一个占位符,用于在XML中查看。 - Abhi
没看到你要我的代码,抱歉...我现在发布。 - Dimness
不确定问题出在哪里,无论我尝试什么都有效。起初我没有看到文本,直到我用android:text替换了tools:text,并删除了android:maxLines="2",这样它就可以将所有文本放在屏幕上。如果更改这两个内容没有帮助,抱歉。 - Dimness

0
在我的情况下,RecyclerView是由适配器填充的,并且适配器具有正确的约束和0dp宽度。然而,它仍然无法正常工作。
我最终所做的是将RecyclerView元素的layout_width更改为match_parent,而不是wrap_content。
RecyclerView
<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/stepsList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

Adapter.xml

<androidx.constraintlayout.widget.ConstraintLayout
    ...
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/steps"
    android:paddingStart="10dp"
    android:paddingEnd="10dp">


    <ImageView
        android:id="@+id/stepsImg"
        android:layout_width="150dp"
        android:layout_height="80dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/stepsText"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_marginBottom="3dp"
        android:layout_marginStart="5dp"
        android:gravity="center"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/stepsIma"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接