ConstraintLayout - 避免重叠

ConstraintLayout - avoid overlapping

有一个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" 那么视图将相互重叠..

我需要确保小文本有一个 "wrap content",正如我在上面的屏幕截图中所做的那样,但是对于较大的文本,文本视图必须最多占据大约父级水平方向的 40%。好吧,文本也没有传输 - 我做 android: ellipsize =" end "android: singleLine =" true.

它应该是这样的(在 Photoshop 中编辑以供演示): 如何使用 ConstraintLayout 或如果不能 - 使用其他布局?

You can do like this :

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

您也可以使用 指南layout_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>

以下属性有效:

app:layout_constrainedWidth="true"

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

WRAP_CONTENT : enforcing constraints (Added in 1.1) If a dimension is set to WRAP_CONTENT, in versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attribute:

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

如果约束来自 left/right

    set vertical guideline
    set component layout_width = 0dp

如果约束来自top/bottom

    set horizontal guideline
    set component layout_height = 0dp