【问题标题】:TextView exceed Constraint if text is too long in ConstraintLayout如果 ConstraintLayout 中的文本太长,TextView 会超过 Constraint
【发布时间】:2020-06-22 17:52:48
【问题描述】:

我有一个TextView 向左对齐并垂直居中。两组ImageViewTextView 都向右对齐,有两种布局配置,一组在右侧,另一组在左侧。

hello world 文本可以很长。我希望它扩展以填充整个宽度而不覆盖左侧文本。

我添加了app:layout_constraintStart_toEndOf="@id/text1" 以限制右侧组(图像+文本)不与左侧文本重叠。但是,它的行为不像我预期的那样。

如何仅使用 ConstraintLayout 使其不重叠?

Text1 如果正确的文本太长,将被覆盖,这是不期望的。请注意,第二行的图片也不见了。

代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Text1" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/row1_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@id/row1_image"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/text1"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Hello World" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/row1_image"
        android:layout_width="22dp"
        android:layout_height="22dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/row1_text"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@drawable/ic_confirm" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/horizontal_barrier"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="row1_text,row1_image" />


    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/row2_image"
        android:layout_width="22dp"
        android:layout_height="22dp"
        app:layout_constraintEnd_toStartOf="@id/row2_text"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/text1"
        app:layout_constraintTop_toTopOf="@id/horizontal_barrier"
        tools:src="@drawable/ic_confirm" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/row2_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/row2_image"
        app:layout_constraintTop_toTopOf="@id/horizontal_barrier"
        tools:text="Hello World" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果正确的文本太长,预计如下所示。可以通过LinearLayout和RelativeLayout来实现

【问题讨论】:

  • 你能贴一张想要的结果的图片吗?从你的措辞我无法理解你想要达到什么目的
  • @LenaBru 我编辑了问题以详细说明。有帮助吗?

标签: android android-layout constraints android-constraintlayout


【解决方案1】:

请检查 Constraintlayout 版本。使用app:layout_constrainedWidth="true" android:layout_width="wrap_content"

【讨论】:

    【解决方案2】:

    您可以使用Guidelines 来阻止您的文本超出需要的范围。

    我们以这个布局为例:

    <?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"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity">
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="longgggggg texttttttttttttttttexttttttttttttttttexttttttttttttttttexttttttttttttttt"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.18" />
    

    并且由于两个文本视图上的 app:layout_constraintGuide_percent="0.18"android:layout_width="0dp" 长 textView 不会退出其约束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 2018-08-16
      • 2014-10-02
      • 1970-01-01
      • 2015-03-15
      • 2017-01-10
      • 2019-04-13
      相关资源
      最近更新 更多