gpt4 book ai didi

android - 在Android中对齐两个 View 的垂直中心

转载 作者:行者123 更新时间:2023-11-29 18:46:42 28 4
gpt4 key购买 nike

如何在 LinearLayout 或 RelativeLayout 中对齐两个 View 的垂直中心(iOS 等效于 view1.centerY = view2.centerY)?

我正在尝试实现以下布局。如何使第一行中的所有三个 View 在 xml 中垂直对齐?第一行的中间 View 是固定的。我希望左右 View 的垂直中心线等于中间 View 的垂直中心线。

enter image description here

最佳答案

约束布局:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ConstraintLayout">

<View
android:id="@+id/left"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:background="#FF2600"
app:layout_constraintBottom_toBottomOf="@+id/center"
app:layout_constraintEnd_toStartOf="@+id/center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/center" />

<View
android:id="@+id/center"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#0633FF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<View
android:id="@+id/right"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:background="#FF40FF"
app:layout_constraintBottom_toBottomOf="@+id/center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/center"
app:layout_constraintTop_toTopOf="@+id/center" />
</android.support.constraint.ConstraintLayout>

Design

关于android - 在Android中对齐两个 View 的垂直中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51611503/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com