ConstraintLayout - 避免重叠

21

有一个 ConstraintLayout 布局:

<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="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="small text"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <Button
        android:ellipsize="end"
        android:singleLine="true"
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="small text"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

如下所示: введите сюда описание изображения 现在还好,但如果我将android:text="small text"更改为android:text="big teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext",则视图将重叠。

我需要确保小文本使用“包装内容”,就像上面的屏幕截图一样,但是对于较大的文本,文本视图必须水平占据父级的约40%。同时也要防止文本被转移-我使用android: ellipsize =" end "android: singleLine =" true

这是应该是这样的(在Photoshop中进行了编辑以演示): введите сюда описание изображения 用ConstraintLayout或其他布局如何实现?

5个回答

27

您还可以像下面的示例一样使用Guidelinelayout_constraintWidth_default属性来完成此操作。

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


    <Button
        android:id="@+id/button10"
        android:layout_width="0dp"
        app:layout_constraintWidth_default="wrap"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="sdtessdsdsdsdsdsdsdsddsdsdxt"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintHorizontal_bias="0"
        android:layout_marginTop="8dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline"
        android:layout_marginRight="8dp" />

    <Button
        android:ellipsize="end"
        android:singleLine="true"
        android:id="@+id/button11"
        android:layout_width="0dp"
        app:layout_constraintWidth_default="wrap"
        android:layout_height="wrap_content"
        android:text="ddddddsdssdsdsdsdsdsdddt"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginRight="-1dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintLeft_toLeftOf="@+id/guideline" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>

2
只有当您使用'app:layout_constraintRight_toRightOf'和'app:layout_constraintLeft_toLeftOf'时,这才能正常工作。不要使用->'constraintStart_toStarttOf'和'constraintEnd_toEndOf'。 - LinuxFelipe-COL

25
以下属性有效:
app:layout_constrainedWidth="true"

https://developer.android.com/reference/android/support/constraint/ConstraintLayout

WRAP_CONTENT:强制约束(1.1版本中添加) 如果将一个维度设置为 WRAP_CONTENT,那么在1.1版本之前,它们将被视为字面维度 -- 这意味着,约束不会限制结果的维度。虽然通常这足够了(而且更快),但在某些情况下,您可能希望使用 WRAP_CONTENT,同时保持强制约束以限制结果的尺寸。在这种情况下,您可以添加相应的属性:

app:layout_constrainedWidth=”true|false” app:layout_constrainedHeight=”true|false”


3
你可以这样做:
<android.support.constraint.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">

<Button
    android:id="@+id/button10"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:gravity="center_vertical"
    android:text="small text"
    android:layout_marginRight="20dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/button11"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button11"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:maxLines="1"
    android:layout_marginLeft="20dp"
    android:gravity="center_vertical"
    android:text="small textsfdgdfjkghkdfhgjkdfhgkhgkhkjjkgfkgjkfgjkgjkjgfdkj"
    app:layout_constraintLeft_toRightOf="@+id/button10"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button10" />


如果你想要使用 android:ellipsize="end",那么你必须给按钮设置固定的宽度。 - Shweta Chauhan
1
这并不真正起作用。其中一个按钮将依赖于另一个按钮,而那个按钮将决定位置。 - TheLibrarian

2
如果限制来自左侧/右侧。最初的回答。
    set vertical guideline
    set component layout_width = 0dp

如果约束来自顶部/底部
    set horizontal guideline
    set component layout_height = 0dp

0

我建议使用水平链和'app:layout_constraintWidth_percent'。

'spread_inside'链类型会使按钮粘附在父边缘上,它添加在链中的第一个按钮上。 enter image description here

'app:layout_constraintWidth_percent'在每个按钮上扮演指南的角色。它在两个按钮上都设置为0.5。这将使它们各自占用可用水平空间的最大一半。

 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <Button
      android:id="@+id/button10"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintWidth_max="wrap"
      app:layout_constraintWidth_percent="0.5"
      android:ellipsize="end"
      android:singleLine="true"
      android:text="bguigui uegerger er ergre greg  gre grefufifu "
      app:layout_constraintHorizontal_chainStyle="spread_inside"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toStartOf="@+id/button11"/>

  <Button
      android:id="@+id/button11"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintWidth_max="wrap"
      app:layout_constraintWidth_percent="0.5"
      android:ellipsize="end"
      android:singleLine="true"
      android:text="aaa jjjk kkkl"
      app:layout_constraintStart_toEndOf="@+id/button10"
      app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

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