ConstraintLayout - 如何垂直对齐两个视图的中心

2024-01-16

我有两个视图 - A 和 B。它们有不同的高度。

如何在内部垂直对齐这些视图的中心ConstraintLayout?

例如,在下面的 XML 中,我想要中心img_change_picture与中心对齐txt_change_picture.

<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"
>

<android.support.constraint.Guideline
    android:id="@+id/guideline_icons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.1"/>

<android.support.constraint.Guideline
    android:id="@+id/guideline_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.2"/>

<ImageView
    android:id="@+id/img_change_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toEndOf="@id/guideline_icons"
    app:layout_constraintTop_toBottomOf="@id/img_header"
    android:layout_marginTop="@dimen/settings_main_vertical_spacing"
    app:srcCompat="@drawable/change_picture"/>

<TextView
    android:id="@+id/txt_change_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toEndOf="@id/guideline_text"
    android:text="@string/settings_main_change_picture"
    />

</android.support.constraint.ConstraintLayout>

  1. 如果没有指南,请将属性放在需要垂直对齐的每个视图上。

    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    
  2. 有指导

添加水平辅助线并使其垂直居中layout_constraintGuide_percent.

<androidx.constraintlayout.widget.Guideline
     android:id="@+id/guideline"
     android:layout_width="1dp"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     app:layout_constraintGuide_percent="0.5"/>

对于每个视图,将它们锚定到带有属性的指南

app:layout_constraintTop_toTopOf="@id/guideline"
app:layout_constraintBottom_toBottomOf="@id/guideline"

使用参考线,您将拥有更大的灵活性,因为您可以通过移动参考线轻松更改所有视图位置。

Update:

要将一个视图与另一个视图垂直居中对齐,您只需在属性中引用视图 id 即可。对齐txt_change_picture垂直居中img_change_picture,您可以像这样更改布局:

<TextView
    android:id="@+id/txt_change_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toEndOf="@id/guideline_text"
    android:text="@string/settings_main_change_picture"

    app:layout_constraintTop_toTopOf="@id/img_change_picture"
    app:layout_constraintBottom_toBottomOf="@id/img_change_picture"

/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ConstraintLayout - 如何垂直对齐两个视图的中心 的相关文章

  • 在我的 Android 应用程序中使用 ServerValue.TIMESTAMP

    我读过很多相关的 stackoverflow 问题 ServerValue TIMESTAMP 但我不知道如何在我的应用程序中使用它 我需要获取帖子创建的时间戳 时间戳应该添加到与帖子的 uid 作者等相同的位置 代码片段其中写这篇文章Fi
  • Android上如何模拟后台Activity因内存不足而被系统杀死的过程?

    我正在处理 内存不足 不再有后台进程 问题 当这种情况发生时 我的活动处于后台并被杀死 我正在尝试保存并加载实例状态来解决它 但因为它并不是每次都会发生 在这种情况下我应该如何测试我的活动 Thanks 您可以通过 adb 强制进程终止 g
  • 导入已经创建的sqlite数据库(xamarin)

    我正在使用 Xamarin 想知道如何导入我已经创建的 sqlite 数据库 到目前为止 我已将其添加到资产文件夹中 但不知道下一步从哪里开始 string localPath Path Combine System Environment
  • 如何在 android 中启动“添加联系人”活动

    您能告诉我如何在 Android 中启动 添加联系人 活动吗 谢谢 API 级别 5 及以上解决方案 Add listener so your activity gets called back upon completion of act
  • API29 上不推荐使用 setColorFilter

    我使用以下行来更改 VectorDrawable 的颜色 mydrawable getBackground setColorFilter color PorterDuff Mode SRC ATOP 这很好用 尽管它现在已被弃用 文档建议我
  • Android PhoneGap 插件,UI 选项卡栏,调整 WebView 大小

    我正在创建一个美味的 PhoneGap 插件 希望一旦它能被打开 准备好了 插件基本完成了 我只需要一个漂亮的用户界面 相互作用 简而言之 我想创建一个 本机 android 工具栏组件 如果您实现 PhoneGap UIControls
  • Google 地图删除标记路线上下文菜单

    我使用 Android Studio 的 Google 地图模板启动了一个新项目 并在地图上添加了一个标记 LatLng location new LatLng lat lng Marker marker mMap addMarker ne
  • 使用 Android Firebase 堆栈推送通知

    我开发了使用 Firebase 接收推送通知的 Android 应用程序 我的代码基于 Firebase Google 官方文档 https firebase google com docs cloud messaging android
  • Android Fragment onCreateView 与手势

    我正在尝试在片段中使用手势 我在 FragmentActivity 中有以下内容来处理我的详细信息片段 我试图发生的情况是 当在视图上检测到滑动时 将该视图内的数据替换为上一个或下一个条目 如果有更好的方法来处理这个问题 我完全同意 然而
  • 如何在 NumberPicker 中一次显示 3 个以上的值

    我正在创建一个数字选择器 如下图所示 但如果有可用空间 我想显示 3 个以上的值 该选择器有 20 个项目 并且有足够的空间来显示 3 个以上的值 这可以使用 NumberPicker 来完成吗 只需以编程方式设置numberPicker
  • 像 WhatsApp 一样发送图片

    我做了一个聊天应用程序 我想添加照片 文件共享我的应用程序中的概念与 WhatsApp 相同 我已经使用该应用程序制作了Xmpp Openfire目前我正在使用此功能进行照片共享 但它并不完全可靠 public void sendFile
  • 适用于 Android 的 Google 云端硬盘\文档 API

    我在几个小时内将 Dropbox 与我的应用程序集成 因为 SDK 描述清晰并且有很好的使用示例 Google Drive 似乎只有一个 一刀切 的 Gdata SDK 它非常重 有很多依赖项 它使我的应用程序的大小增加了三倍 而且不是很直
  • android textview 有字符限制吗?

    我正在尝试在 android TextView 中输入超过 2000 3000 个字符 它不显示任何内容 任何一份指南是否对 android textview 有字符限制或什么 我在G3中做了一些小测试 我发现 如果activtiy布局中有
  • SDK >=26 仍需要 mipmap/ic_launcher.png?

    在 Android 中 有两种指定启动器图标 可以说是应用程序图标 的方法 老 方式 在 mipmap 文件夹中指定不同的 png 文件 通常命名为 ic launcher png 但可以通过以下方式设置名称android icon mip
  • 如何构建自定义摄像机应用程序?

    我正在尝试开发一个自定义摄像机录像机 当我的设备在 Activity 的 beginRecording 中执行 start MediaRecorder 方法时 应用程序崩溃 我不知道出了什么问题 因为我遵循谷歌API指南 http deve
  • Android Root 执行 su 带参数

    我在使用参数执行 su 时遇到问题 包含空格 我的 Command java 看起来像这样 public class Command Process process public String executeCommand String c
  • 如何在Android中创建一个简洁的两栏输入表单?

    我想创建一个整洁的两列输入表单 如下所示 到目前为止我的 xml 布局代码
  • 使用 DataBindingComponent 的 Inflate 方法

    当 Glide 成功渲染图像后 我在更新文本视图时看到此错误 致命异常 java lang IllegalStateException 必需 CustomBinding 类中的 DataBindingComponent 为 null 绑定适
  • 我应该如何在 Android 上使用 Retrofit 处理“无互联网连接”

    我想处理没有互联网连接的情况 通常我会运行 ConnectivityManager cm ConnectivityManager context getSystemService Context CONNECTIVITY SERVICE N
  • 将焦距(以毫米为单位)转换为像素 - Android

    在 Android 中 我当前正在访问camera s焦距通过使用getFocalLength in Camera1 Camera2不是一个选择 我正在尝试完全填充当前的计算 focal length pix focal length m

随机推荐