布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<View
android:id="@+id/view1"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="16dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintBottom_toTopOf="@id/view1"
app:layout_constraintEnd_toEndOf="parent" />
<View
android:id="@+id/view2"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_marginEnd="64dp"
android:layout_marginBottom="256dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
截图:
如何将第一个视图(红框)精确地放置在第二个视图(蓝框)的顶部,使两个视图的中心垂直对齐。
预期布局:(中心应对齐)(在屏幕截图中不对齐)
发布于 2019-05-21 02:01:24
一个简单的解决方案是相对于view2对齐view1。因此,view2将具有某种绝对位置,然后view1将相对于view2对齐自身。
这将是对view1的约束:
app:layout_constraintBottom_toTopOf="@id/view2"
app:layout_constraintEnd_toEndOf="@id/view2"
app:layout_constraintStart_toStartOf="@id/view2"
然后布局应该是:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<View
android:id="@+id/view1"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="16dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintBottom_toTopOf="@id/view2"
app:layout_constraintEnd_toEndOf="@id/view2"
app:layout_constraintStart_toStartOf="@id/view2"/>
<View
android:id="@+id/view2"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_marginEnd="64dp"
android:layout_marginBottom="256dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
另一种解决方案是使用链属性,您可以在docs中看到更多信息
发布于 2019-05-21 01:38:21
是像这样吗?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:orientation="vertical">
<View
android:id="@+id/view2"
android:layout_width="160dp"
android:layout_height="160dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view1"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果您希望能够移动蓝色框,请将红色框更改为:
<View
android:id="@+id/view1"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintBottom_toBottomOf="@+id/view2"
app:layout_constraintEnd_toEndOf="@+id/view2"
app:layout_constraintStart_toStartOf="@id/view2"
app:layout_constraintTop_toTopOf="@id/view2" />
https://stackoverflow.com/questions/56225611
复制相似问题