【问题标题】:View content gets out off bounds in ConstraintLayout在 ConstraintLayout 中查看内容越界
【发布时间】:2020-10-16 16:02:00
【问题描述】:

我需要将两个TextViews 与动态内容对齐,因此需要宽度。 ConstraintLayout 工作良好,直到文本开始换行成多行。左视图在第二个视图的宽度上向左移动(超出左边界)。如何避免这种情况?是某种错误还是我忘记了错误地使用ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="textView2 Lorem ipsum "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="textView1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap" />

</android.support.constraint.ConstraintLayout>

移动视图图像:

【问题讨论】:

  • 我也无法控制自动换行的 TextView 的宽度。我正在处理翻译字符串。我很幸运,我可以缩短翻译以防止自动换行。如果这不起作用,我会尝试的下一个方法是分配固定宽度或调查是否可以分配最大宽度。

标签: android xml android-constraintlayout


【解决方案1】:

layout_constraintLeft_toLeftOf 更改为layout_constraintStart_toStartOf 并将layout_constraintRight_toRightOf 更改为layout_constraintEnd_toEndOf,您将得到如下所示的内容:

这假定 ConstraintLayout 版本为 1.1.0,尽管它可能无关紧要。

【讨论】:

  • 完美运行!谢谢!
【解决方案2】:

您可以将其绑定到从第一个文本视图末尾开始的指南。两种文本视图都可能与指南相关联。

 <androidx.constraintlayout.widget.Guideline
    android:id="@+id/glMiddle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="56dp" />

将 tv1 的结尾与此绑定,并将 tv2 的开头与此绑定

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 2012-12-23
    • 2013-11-09
    • 2018-03-21
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2017-09-21
    相关资源
    最近更新 更多