【问题标题】:constraint layout textview not shown约束布局文本视图未显示
【发布时间】:2019-05-10 05:25:10
【问题描述】:

我正在创建一个简单的回收器视图项布局,其中包含约束布局内的图像和文本视图。我将图像的约束设置在左侧,文本从图像的右侧开始到父级的末尾。但是,当我这样做时,屏幕上不会显示文本视图。有人可以帮帮我吗。提前致谢。

<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" ,我会得到以下结果。

【问题讨论】:

  • 请检查文本颜色 - 我猜它是白色的(您的布局在我的示例应用程序中看起来不错)。 BTW LayoutInspector 是一个很好的工具,如果你的视图没有像他们应该的那样显示
  • @0X0nosugar 文本在我删除 app:layout_constraintEnd_toEndOf="parent" 约束时出现,但右侧的文本超出范围

标签: android android-layout layout android-constraintlayout


【解决方案1】:

我建议您在使用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,你能举个例子吗?谢谢!
  • 是的,尝试将 ImageView 的开始连接到父项的开始,并将其顶部连接到父项的顶部,并将 TextView 的开始连接到 ImageView 的末尾 + 它的顶部到父项的顶部,然后它结束到父级的结束(因为这是您想要的条件之一)。其余所有点用于基本连接,以便在实际设备中运行时显示所有视图。
  • 添加了这 5 个约束。我仍然得到相同的结果。
  • 你能告诉我你的 ConstraintLayout 的依赖版本吗?
  • 使用的是最新的1.1.3
【解决方案2】:

尝试删除

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 元素中删除此属性应该在屏幕上显示所有文本,但行数更多.
  • 是的,它使项目视图更大,但没有显示文本。我尝试从 textView 中删除 parentEnd 约束。它应该是所有文本,但它仍然从右侧超出范围。如果您不介意,您可以发布您的代码。谢谢
  • 尝试替换:tools:text="Homer" With: android:text="Homer"...如何在 textview 中设置文本?
  • 文本在回收器视图适配器类中设置。 tools:text 只是在 xml 中查看的占位符。
  • 没看到你问我的代码,对不起...我现在发布。
【解决方案3】:

在我的例子中,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>

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2018-08-30
    相关资源
    最近更新 更多