约束布局文本视图未显示

时间:2018-12-08 20:15:55

标签: android android-layout layout android-constraintlayout

我正在创建一个简单的回收者视图项目布局,在约束布局中包含图像和textview。我将图像的约束设置在左侧,文本从图像的右侧开始到父级的结尾。但是,执行此操作时,文本视图不会显示在屏幕上。有人可以帮我吗。预先感谢。

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

enter image description here

如果我从textView中删除app:layout_constraintEnd_toEndOf =“ parent”,则会得到以下结果。 enter image description here

2 个答案:

答案 0 :(得分: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]

enter image description here

有关更多信息,请通过: https://codelabs.developers.google.com/codelabs/constraint-layout/index.html#7

希望对您有所帮助。

答案 1 :(得分: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"
    />

相关问题