当前位置: 首页 > article >正文

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

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

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

  •    spread - 元素将被展开(默认样式) 。加权链 - 在spread模式下,如果某些小部件设置为MATCH_CONSTRAINT,则它们将拆分可用空间 
  •    spread_inside - 类似,但链的端点将不会扩展 
  •    packed - 链的元素将被打包在一起。 孩子的水平或垂直偏差属性将影响包装元素的定位
<?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>

显示效果如下所示:


http://www.kler.cn/a/535765.html

相关文章:

  • 简单说一下CAP理论和Base理论
  • [250202] DocumentDB 开源发布:基于 PostgreSQL 的文档数据库新选择 | Jekyll 4.4.0 发布
  • 第二次连接k8s平台注意事项
  • 19C RAC在vmware虚拟机环境下的安装
  • Mac 终端命令大全
  • SpringBoot使用 easy-captcha 实现验证码登录功能
  • 我们究竟畏惧AI什么?
  • 鸿蒙UI(ArkUI-方舟UI框架)- 使用文本
  • Git仓库托管基本使用02——生成公钥
  • 第八天 继续学习ArkTS,掌握基础语法和组件创建
  • 【蓝桥杯—单片机】第十届省赛真题代码题解题笔记 | 省赛 | 真题 | 代码题 | 刷题 | 笔记
  • React 设计模式:实用指南
  • GPU — 8 卡 GPU 服务器与 NVLink/NVSwitch 互联技术
  • 【AI】DeepSeek来了!!!
  • tolua[一]框架搭建,运行example
  • 【Day33 LeetCode】动态规划DP Ⅵ 背包问题
  • SQL Server的安装和简单使用
  • SQL精度丢失:CAST(ce.fund / 100 AS DECIMAL(10, 2)) 得到 99999999.99
  • 【Uniapp-Vue3】z-paging插件组件实现触底和下拉加载数据
  • 【Elasticsearch】random_sampler聚合
  • Leecode刷题C语言之全排列②
  • Spring Boot + Spring AI快速体验
  • Polardb三节点集群部署安装--附虚拟机
  • Linux 设备驱动分类(快速理解驱动架构)
  • 《大模型面试宝典》(2025版) 发布了
  • 国自然地区基金|基于深度学习多模态影像组学智能诊断非酒精性脂肪肝病的研究|基金申请·25-02-06