ConstaintLayout 中的两个元素相互重叠
Two elements in ConstaintLayout overlap each other
我想使用 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>
我会使用 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>
<?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_constraintHorizontal_weight 值
<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>
我想使用 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>
我会使用 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>
<?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_constraintHorizontal_weight 值
<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>