Android 约束布局ConstraintLayout整体链式打包居中显示

Android 用约束布局ConstraintLayout实现将多个控件视作一个整体居中显示,使用 app:layout_constraintHorizontal_chainStyle="packed"实现

chain 除了链条方向有横向和竖向区分外, chain链条上的模式有 3种

  • spread - 元素将被展开(默认样式) 。加权链 - 在spread模式下,如果某些小部件设置为MATCH_CONSTRAINT,则它们将拆分可用空间
  • spread_inside - 类似,但链的端点将不会扩展
  • packed - 链的元素将被打包在一起。 孩子的水平或垂直偏差属性将影响包装元素的定位
XML 复制代码
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- 作为父布局的 ConstraintLayout -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <!-- TextView 1 -->
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:background="#ff0000"
            app:layout_constraintHorizontal_chainStyle="packed"
            android:text="Text 1"
            app:layout_constraintEnd_toStartOf="@+id/textView2"/>

        <!-- TextView 2 -->
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            android:background="#00ff00"
            app:layout_constraintBottom_toBottomOf="parent"
            android:text="Text 2"
            app:layout_constraintStart_toEndOf="@id/textView1" 
            app:layout_constraintEnd_toStartOf="@id/textView3"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:background="#0000ff"
            android:text="Text 3"
            app:layout_constraintLeft_toRightOf="@id/textView2" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

显示效果如下所示:

相关推荐
程序员煊子3 小时前
用 Cursor 从零搭一个 Compose 本地记账 App:实战记录与源码解析
android·kotlin·compose·cursor
alexhilton5 小时前
面向Android开发者的Google I/O 2026
android·kotlin·android jetpack
私人珍藏库6 小时前
【Android】豆图助手-永久HY-模拟微X~zfb各种截图
android·app·工具·软件·多功能
程序员陆业聪7 小时前
Shadow实战接入与生产落地:从零搭建到稳定运行
android
程序员陆业聪7 小时前
Shadow Transform:编译期的魔法——字节码替换实战
android
imuliuliang11 小时前
Laravel6.x核心特性全解析
android·php·laravel
idingzhi12 小时前
A股量化策略日报(2026年05月22日)
android·开发语言·python·kotlin
测试员周周13 小时前
【Appium 系列】第14节-断言与验证 — Validator 的设计
android·人工智能·python·功能测试·ios·单元测试·appium
赏金术士13 小时前
Android 动画对比指南:View 系统 vs Jetpack Compose
android·kotlin·compose
我命由我1234514 小时前
C++ - 面向对象 - 析构函数
android·c语言·开发语言·c++·visualstudio·visual studio·android runtime