如何使用 ConstraintLayout 将多个视图居中在一起?

2024-05-04

背景

谷歌宣布了一个名为“约束布局 http://tools.android.com/tech-docs/layout-editor“这应该是最终的布局,它可以取代所有布局,同时保持平坦(没有嵌套布局)并具有更好的性能。

问题

事实是,除了 Google IO 上提供的视频之外,我几乎没有看到任何可以在这件事上帮助我的教程。

我想做的是,鉴于我在另一个布局中有一个垂直居中的 LinearLayout - 将它们都转换为单个 ConstraintLayout。

毕竟,这就是这个新布局的目的……

我希望处理的布局如下所示:

请注意,中心的视图仅垂直居中,并且 2 个 textView 位于 ImageView 的右侧,ImageView 也垂直居中。

这一切都适用于RelativeLayout,它具有2个TextView的LinearLayout,但我想知道如何将它们转换为单个ConstraintLayout。

下面是我所展示的 XML 示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="wrap_content"
    android:minHeight="?attr/listPreferredItemHeightSmall">

    <ImageView
        android:id="@+id/appIconImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="4dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="4dp"
        android:layout_marginStart="2dp"
        android:adjustViewBounds="true"
        android:src="@android:drawable/sym_def_app_icon"
        tools:ignore="ContentDescription"/>

    <LinearLayout
        android:id="@+id/appDetailsContainer"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/appIconImageView"
        android:layout_toLeftOf="@+id/overflowView"
        android:layout_toRightOf="@+id/appIconImageView"
        android:layout_toStartOf="@+id/overflowView"
        android:orientation="vertical">

        <TextView
            android:id="@+id/appLabelTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:text="label"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textDirection="locale"
            tools:ignore="HardcodedText,UnusedAttribute"/>

        <TextView
            android:id="@+id/appDescriptionTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:minLines="3"
            android:text="description"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textDirection="locale"
            tools:ignore="HardcodedText,UnusedAttribute"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/overflowView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:padding="10dp"
        app:srcCompat="@drawable/ic_more_vert_black_24dp"

        tools:src="@drawable/ic_more_vert_black_24dp"
        tools:ignore="ContentDescription"/>

    <ImageView
        android:id="@+id/isSystemAppImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/overflowView"
        android:layout_alignLeft="@+id/overflowView"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/overflowView"
        android:layout_alignStart="@+id/overflowView"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        app:srcCompat="@drawable/ic_warning_black_24dp"
        tools:ignore="ContentDescription"
        tools:src="@drawable/ic_warning_black_24dp"/>

</RelativeLayout>

我尝试过的

我尝试阅读一些文章并观看谷歌的一些视频:

  • https://codelabs.developers.google.com/codelabs/constraint-layout/index.html#0 https://codelabs.developers.google.com/codelabs/constraint-layout/index.html#0
  • https://www.youtube.com/watch?v=sO9aX87hq9c https://www.youtube.com/watch?v=sO9aX87hq9c
  • https://youtu.be/csaXml4xtN8?t=1693 https://youtu.be/csaXml4xtN8?t=1693

这没有帮助,所以我尝试使用它,希望我能自己找到如何使用它。 但我不知道该怎么做。我尝试使用该功能来转换布局,但这会使视图变得一团糟,并增加了我不希望有的额外边距。

问题

如何将 2 个布局转换为单个 ConstraintLayout ?


看看我的回答here https://stackoverflow.com/q/37507463/4378400.

ContraintLayout https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html包含一个功能——Chains- 这使得实现你所要求的成为可能:

链在单轴(水平或水平方向)上提供类似群体的行为 垂直)。

如果一组小部件通过以下方式链接在一起,则它们被视为一条链: 双向连接

一旦创建了一条链,就有两种可能性:

  • 将元素散布在可用空间中
  • 链也可以“打包”,在这种情况下,元素被分组在一起

至于你的箱子,你必须打包你的label and descriptionTextViews 并将它们垂直居中在您的父级中:

(确保您使用的版本ConstraintLayout支持链)

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintLeft_toRightOf="@+id/imageView2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"/>

    <TextView
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button\nMkay"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/imageView2"
        app:layout_constraintTop_toBottomOf="@+id/textView"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>

更新 25-06-2019 (@Saeid Z https://stackoverflow.com/users/7766540/saeid-z):

现在在约束布局 1.1.3 中我们必须使用app:layout_constraintHorizontal_chainStyle="packed"代替app:layout_constraintVertical_chainPacked="true"

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

如何使用 ConstraintLayout 将多个视图居中在一起? 的相关文章

随机推荐

  • 在不滚动的情况下对 UITableViewCell 的高度变化进行动画处理

    我知道如何使用此处所示的方法对 UITableViewCell 的高度变化进行动画处理 当 UITableViewCell 被选中时 你能用动画来改变高度吗 https stackoverflow com questions 460014
  • 设置 CMake OBJECT 库的输出目录

    在我的 CMake 文件中 我指定了一个对象库 add library core OBJECT sourcefiles 我在共享库中进一步引用了这组目标文件 add library sharedlib SHARED
  • Passenger 无法识别本地安装的 gem,可使用打包的 gem

    这是一个生产服务器 我已经安装了 Passenger 并且大部分情况下都可以正常工作 然而 我总是必须将宝石与项目打包在一起 如果不这样做 我会收到以下错误消息 Missing the Rails 2 3 8 gem Please gem
  • logback 消息字段可以被截断/修剪为 n 个字符吗?

    有时会看到巨大的日志消息 并且并不总是能够 轻松 自动换行 有没有办法截断 message比如说 80 个字符logback xml 看一下格式修饰符部分 From http logback qos ch manual layouts ht
  • 如何给DArray的元素设置值?

    我正在探索 Julia 的并行计算并尝试了以下方法 a dzeros 5 a 1 5 但刚刚收到此错误 setindex not defined for DArray Float64 1 Array Float64 1 嗯 我以为手册上说s
  • 如何在存储过程中调用存储过程(带2个参数)?

    我有具有相同参数 服务器名称和日期 的存储过程 我想编写一个存储过程并在该 SP 中执行它们 称为 SP All CREATE PROCEDURE dbo SP All AS BEGIN exec sp 1 myDate datetime
  • IEEE-754 32 位(单精度)指数 -126 而不是 -127

    我知道我是否有这样的号码 1 1001 0001 0011 0011 0000 0001 0101 000 1 sign bit 8 bit biased exponent 23 bit fraction mantissa 我可以通过从有偏
  • Math.random 生成多少熵?

    我想生成一个非常大的随机数 我不需要这个号码来保证加密安全 因此 我没有使用crypto getRandomValues https developer mozilla org en US docs Web API RandomSource
  • 处理原始 HTTP 请求内容

    我正在 ASP NET 中做一个电子商务解决方案 它使用PayPal 网站支付标准 https www paypal com IntegrationCenter ic standard home html服务 除此之外 我还使用他们提供的服
  • 使用 Git 和 Eclipse 管理 Android 项目

    我相信我有一个非常常见的问题 它会影响具有多个应用程序版本的开发人员 在我的例子中 我有两个 付费版本和免费版本 为了管理这两个版本 我使用具有 2 个不同分支的同一个 git 项目 免费和付费 然而 我的源代码包名称彼此不同 如预期 并且
  • 如何在neo4j中显示屏幕上的所有节点

    我有近 5000 个节点Recipes和 5 个节点Meal Types在 Neo4j 数据库中 目前他们之间没有任何关系 我正在下面运行 CQL MATCH n RETURN n LIMIT 100000 这运行良好 但它返回与相关的节点
  • 投影 3D 网格的 2D 轮廓算法

    给定 一个 3D 网格 由一组顶点和三角形定义 并用这些点构建网格 问题 找到任意平面上投影的任意旋转网格的二维轮廓 投影很容易 挑战在于找到平面中投影三角形边的 外壳 我需要一些有关研究该算法的输入 指针的帮助 为简单起见 我们可以假设
  • 在opencv中发现凸性缺陷? [根据给定的输入图像崩溃..]

    我有一个计算图像凸包的程序 我正在尝试使用此信息来计算fingers存在于输入图像中 从一些冲浪中我发现做到这一点的方法 数手指 是 寻找轮廓 凸包 凸性缺陷 但我在使用凸性缺陷函数时遇到了麻烦 它编译得很好 但在运行时程序会因某些输入图像
  • Linux 中不使用 C++ 的 C 异常处理

    Linux 是否提供了 C 语言的异常处理而不求助于 C 或者 实现此类异常处理的最佳方法是什么 目标是避免检查每个调用的函数的返回码 而是执行类似于 C 的线程安全且易于移植的操作 您可以通过为其编写信号处理程序来处理信号 GNU 记录的
  • gettext 中的“msgstr”有多个“msgid”

    是否可以制作两个或多个msgids匹配一个msgstr 例如 两者 列表 空 and 你正在接近gettext以错误的方式 它是如何工作的 msgid每个条目都需要 msgctxt是可选的 用于区分两个msgid内容相同但翻译可能不同的记录
  • 将 Recyclerview 与 DiffUtil.ItemCallback 一起使用不会滚动到顶部?

    我会搜索项目 每次从服务器获取时 当我搜索 o 时 它起作用 然后 ot 它再次起作用 作为 o 搜索的结果 我有 20 个项目 如果是 ot 我有 10 个项目 这是这 20 个项目的一部分 我确实删除了 EditText 中的 t 它显
  • 如何将 model.summary() 保存到 Keras 中的文件?

    有model summary 方法 https keras io models about keras models 在喀拉斯 它将表打印到标准输出 是否可以将其保存到文件中 如果您想要摘要的格式 您可以传递print功能为model su
  • 如何从 Grails 应用程序的 URL 中删除应用程序名称?

    我有一个应用程序在这样的 URL 上运行 http myapp mydomain com myapp http myapp mydomain com myapp 我不想要 myappURL 中的部分 那么如何去掉应用程序名称呢 我只想 ht
  • sqlalchemy:无法在拆卸时删除数据库

    我正在尝试使用 pytest 进行 sqlalchemy 但遇到以下问题 pytest fixture scope function def my session my db request from my models import Se
  • 如何使用 ConstraintLayout 将多个视图居中在一起?

    背景 谷歌宣布了一个名为 约束布局 http tools android com tech docs layout editor 这应该是最终的布局 它可以取代所有布局 同时保持平坦 没有嵌套布局 并具有更好的性能 问题 事实是 除了 Go