ConstraintLayout - 避免重叠

ilw*_*ilw 10 android text textview android-constraintlayout

有一个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>
Run Code Online (Sandbox Code Playgroud)

它显示如下: введитесюдаописаниеизображения 现在没关系,但是如果我改为 android:text="small text",android:text="big teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext"那么视图会相互重叠..

我需要确保使用小文本有一个"换行内容",正如我在上面的屏幕截图中所做的那样,但是如果文本较大,则文本视图必须占据父级的大约40%的水平.那么文本也没有转移 - 我愿意android: ellipsize =" end "android: singleLine =" true.

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

Pav*_*van 22

您也可以使用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>
Run Code Online (Sandbox Code Playgroud)

  • 仅当您使用 'app:layout_constraintRight_toRightOf' 和 'app:layout_constraintLeft_toLeftOf' *不要使用* -&gt; 'constraintStart_toStarttOf' 和 'constraintEnd_toEndOf' 时,这才有效 (2认同)

Tam*_*ath 17

以下属性有效:

app:layout_constrainedWidth="true"
Run Code Online (Sandbox Code Playgroud)

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

WRAP_CONTENT :强制约束(在 1.1 中添加)如果维度设置为 WRAP_CONTENT,则在 1.1 之前的版本中,它们将被视为文字维度——这意味着约束不会限制结果维度。虽然通常这已经足够(并且更快),但在某些情况下,您可能希望使用 WRAP_CONTENT,但继续强制执行约束以限制结果维度。在这种情况下,您可以添加相应的属性之一:

应用程序:布局_约束宽度=“真|假” 应用:布局_约束高度=“真|假”