首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在约束布局中将两个视图的中心对齐的情况下将一个视图放在另一个视图的顶部?

如何在约束布局中将两个视图的中心对齐的情况下将一个视图放在另一个视图的顶部?
EN

Stack Overflow用户
提问于 2019-05-21 01:28:02
回答 2查看 49关注 0票数 0

布局:

代码语言:javascript
复制
<?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>

截图:

如何将第一个视图(红框)精确地放置在第二个视图(蓝框)的顶部,使两个视图的中心垂直对齐。

预期布局:(中心应对齐)(在屏幕截图中不对齐)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-21 02:01:24

一个简单的解决方案是相对于view2对齐view1。因此,view2将具有某种绝对位置,然后view1将相对于view2对齐自身。

这将是对view1的约束:

代码语言:javascript
复制
app:layout_constraintBottom_toTopOf="@id/view2"
app:layout_constraintEnd_toEndOf="@id/view2"
app:layout_constraintStart_toStartOf="@id/view2"

然后布局应该是:

代码语言:javascript
复制
<?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中看到更多信息

票数 1
EN

Stack Overflow用户

发布于 2019-05-21 01:38:21

是像这样吗?

代码语言:javascript
复制
<?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>

如果您希望能够移动蓝色框,请将红色框更改为:

代码语言:javascript
复制
  <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" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56225611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档