ConstaintLayout中的两个元素相互重叠

时间:2018-02-01 09:14:17

标签: android 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.holodynskyi.v.test.MainActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_margin="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:text="Text111111111111111111111111"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:text="Text2"
            app:layout_constraintEnd_toEndOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:1)

我会使用Guideline来定义TextView1和TextView2之间的不可见限制。

<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="wrap_content">

        <TextView
            android:id="@+id/text1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:text="Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1Text1"
            app:layout_constraintEnd_toStartOf="@id/endOfText1"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:text="Text2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/endOfText1" />

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

    </android.support.constraint.ConstraintLayout>

答案 1 :(得分:1)

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="0dp"
        app:layout_constraintHorizontal_weight="1"
        android:layout_marginEnd="10dp"
        android:layout_height="wrap_content"
        android:text="abcdefghijklmnopqrstuvwxyzhhjkjhjkhadlkhjajsdlkjalkdjlkajdlkjaldkjaljljadjaldjaljd"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/text2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        app:layout_constraintHorizontal_weight="2"
        android:layout_height="wrap_content"
        android:text="abcdefghijklmnopqrstuvwxyzhhjkjhjkhadlkhjajsdlkjalkdjlkajdlkjaldkjaljljadjaldjaljd"
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

根据您的要求修改app:layout_constraintHorizo​​ntal_weight值

答案 2 :(得分:0)

<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:context="com.holodynskyi.v.test.MainActivity">

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:text="any text"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:gravity="center"
            android:text="any text 2"
            app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>