在 ConstraintLayout 中将一个视图放在另一个视图的右侧
Putting a view to the right of another view in ConstraintLayout
我希望 textfield3 位于 textfield2 的左侧。
app:layout_constraintRight_toLeftOf="@id/txt2" 不起作用。
或者我在这里错过了什么。
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:layout_margin="10dp"
android:text="TEXTFIELD 1"/>
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/txt1"
app:layout_constraintEnd_toEndOf="parent"
android:background="@android:color/holo_orange_dark"
android:layout_margin="10dp"
android:text="TEXTFIELD 2"/>
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/txt1"
app:layout_constraintRight_toLeftOf="@id/txt2"
android:background="@android:color/holo_red_light"
android:layout_margin="10dp"
android:text="TEXTFIELD 3"/>
</android.support.constraint.ConstraintLayout>
输出:
在ConstraintLayout
中,每个小部件都必须在水平和垂直方向上进行约束。如果不是,则结果将关闭。您的某些 TextView
缺少约束。尝试如下操作:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="@android:color/darker_gray"
android:text="TEXTFIELD 1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:background="@android:color/holo_orange_dark"
android:text="TEXTFIELD 2"
app:layout_constraintStart_toEndOf="@+id/txt3"
app:layout_constraintTop_toTopOf="@+id/txt3" />
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/holo_red_light"
android:text="TEXTFIELD 3"
app:layout_constraintStart_toStartOf="@+id/txt1"
app:layout_constraintTop_toBottomOf="@+id/txt1" />
</android.support.constraint.ConstraintLayout>
实际上我的布局是正确的,我发布的屏幕截图来自Android Studio 3.0 的布局编辑器。但是当我在模拟器上 运行 时,text3 实际上是粘在 text2 的左边。
肯定是布局编辑器的一个错误。
我希望 textfield3 位于 textfield2 的左侧。
app:layout_constraintRight_toLeftOf="@id/txt2" 不起作用。 或者我在这里错过了什么。
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:layout_margin="10dp"
android:text="TEXTFIELD 1"/>
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/txt1"
app:layout_constraintEnd_toEndOf="parent"
android:background="@android:color/holo_orange_dark"
android:layout_margin="10dp"
android:text="TEXTFIELD 2"/>
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/txt1"
app:layout_constraintRight_toLeftOf="@id/txt2"
android:background="@android:color/holo_red_light"
android:layout_margin="10dp"
android:text="TEXTFIELD 3"/>
</android.support.constraint.ConstraintLayout>
输出:
在ConstraintLayout
中,每个小部件都必须在水平和垂直方向上进行约束。如果不是,则结果将关闭。您的某些 TextView
缺少约束。尝试如下操作:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="@android:color/darker_gray"
android:text="TEXTFIELD 1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:background="@android:color/holo_orange_dark"
android:text="TEXTFIELD 2"
app:layout_constraintStart_toEndOf="@+id/txt3"
app:layout_constraintTop_toTopOf="@+id/txt3" />
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/holo_red_light"
android:text="TEXTFIELD 3"
app:layout_constraintStart_toStartOf="@+id/txt1"
app:layout_constraintTop_toBottomOf="@+id/txt1" />
</android.support.constraint.ConstraintLayout>
实际上我的布局是正确的,我发布的屏幕截图来自Android Studio 3.0 的布局编辑器。但是当我在模拟器上 运行 时,text3 实际上是粘在 text2 的左边。
肯定是布局编辑器的一个错误。