首页 > 其他分享 >Android经典实战之约束布局ConstraintLayout的实用技巧和经验

Android经典实战之约束布局ConstraintLayout的实用技巧和经验

时间:2024-08-15 22:55:06浏览次数:10  
标签:实用技巧 layout 布局 视图 ConstraintLayout ConstraintSet Android 约束

本文首发于公众号“AntDream”,欢迎微信搜索“AntDream”或扫描文章底部二维码关注,和我一起每天进步一点点

ConstraintLayout 是 Android 中一种强大的布局管理器,能够帮助你创建复杂而灵活的布局。它通过约束系统将一个 View 的位置和大小与其他 View 或父布局联系起来,使得布局代码更加简洁且易于维护。

ConstraintLayout 概述

ConstraintLayout 是一种基于约束的布局方式,与传统的布局(如 LinearLayout、RelativeLayout)相比,具有更高的灵活性和性能。它允许你在视图之间创建多种多样的约束条件,比如对齐、比例、偏移等。

基本用法

要使用 ConstraintLayout 需要在布局文件中声明它,通常使用 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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello, ConstraintLayout!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

在这个例子中,TextView 的宽度设为 0dp,表示它会根据约束条件自动调整大小。它的左右边缘约束到父布局左右边缘,顶部和底部分别约束到父布局顶部和底部,这样它就会在父布局中居中显示。

常用属性

  • layout_constraintLeft_toLeftOflayout_constraintRight_toRightOflayout_constraintTop_toTopOflayout_constraintBottom_toBottomOf
    用于设置视图四个边缘的约束,这些约束可以是父布局或者其他视图的边缘。

  • layout_constraintHorizontal_biaslayout_constraintVertical_bias
    控制视图在其约束区间内的偏移量,取值范围是 0.0 - 1.0,默认为 0.5(居中)。

  • layout_constraintWidth_percentlayout_constraintHeight_percent
    通过百分比来定义视图的宽度和高度。

  • layout_constraintDimensionRatio
    设置视图的宽高比,如 "1:1" 表示正方形。

辅助工具

ConstraintLayout 提供了 ConstraintSet 和 ConstraintLayout Editor(在 Android Studio 中),可以更方便地创建和修改布局。

ConstraintSet

ConstraintSet 允许你通过代码动态地改变布局约束,以下是一个简单示例:

import androidx.constraintlayout.widget.ConstraintSet
import androidx.constraintlayout.widget.ConstraintLayout

val constraintLayout: ConstraintLayout = findViewById(R.id.constraintLayout)
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayout)

// 创建新的约束
constraintSet.connect(R.id.textView, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0)
constraintSet.connect(R.id.textView, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0)

// 应用新的约束
constraintSet.applyTo(constraintLayout)

实用技巧和经验

  1. 尽可能多用 0dp(match constraints):
    当你希望视图根据其约束条件自动调整大小时,使用 0dp 作为宽度或高度。

  2. 谨慎使用 wrap_content:
    wrap_content 可能会导致性能问题,考虑用 match constraints 来代替。

  3. 使用 chains:
    在需要多个视图沿某个方向排列时,可以使用链(horizontal chain,vertical chain),这比线性布局更高效。

  4. 减少嵌套布局:
    ConstraintLayout 可以大幅减少传统布局中的嵌套层级,从而提升性能。

  5. ConstraintLayout Editor:
    使用 Android Studio 提供的可视化工具来设计和调整你的 ConstraintLayout,这会让布局设计更直观。

  6. 性能调优:
    ConstraintLayout 自身性能已经不错,但确保你不为每个小部件使用过多的复杂约束,这样可以保持较好的渲染性能。

  7. Barrier 和 Guideline:
    使用 Barrier 可以实现动态的视图边界管理,而 Guideline 可以让你更容易地对齐视图。

<androidx.constraintlayout.widget.Barrier
    android:id="@+id/barrier"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:barrierDirection="end"
    app:constraint_referenced_ids="button1,button2" />

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

总结

通过合理使用和理解 ConstraintLayout 及其提供的丰富 API 和工具集,可以让你高效地创建和管理复杂且灵活的 Android 布局。希望这些讲解和技巧能帮助你更好地使用 ConstraintLayout。


欢迎关注我的公众号AntDream查看更多精彩文章!

AntDream

标签:实用技巧,layout,布局,视图,ConstraintLayout,ConstraintSet,Android,约束
From: https://blog.csdn.net/myth13141314/article/details/141134889

相关文章

  • Android Qcom USB Driver学习(三)
    usbhub区分端口USB子系统拓扑浅析USBIDDatabaseVendorIDandProductIDusbusb1:NewUSBdevicefound,idVendor=1d6b,idProduct=0002,bcdDevice=4.19usbusb1:NewUSBdevicestrings:Mfr=3,Product=2,SerialNumber=1usbusb1:Product:xHCIHostController......
  • Android Qcom USB Driver学习(二)
    BCv1.2充电规范BatteryChargingSpecificationUSBport如何识别不同的Charger类型USBCharger类型USB_SDP_CHARGERPOWER_SUPPLY_TYPE_USB(StandardDownstreamPort)USBChargerUSB_CDP_CHARGERPOWER_SUPPLY_TYPE_USB_CDP(ChargingDownstreamPort)USBChar......
  • Android T(13) The app is granted permissions by default
    对比Android11,frameworks\base\services\core\java\com\android\server\pm\permission文件夹下,多了个PermissionManagerServiceImpl.java.有一部分关于权限的处理,移到了这个文件中.比如:restorePermissionState(...)allappgrantedpermissionsbydefault+++b/frameworks......
  • 高德地图SDK Android版开发 5 地图交互操作
    高德地图SDKAndroid版开发5地图交互操作前言控件控件介绍接口UiSettingsAMapOptions示例代码Logo指南针比例尺缩放按钮效果图地图手势地图手势方法说明地图手势开关指定屏幕中心点的手势操作地图手势事件地图点击事件监听接口地图长按事件监听接口地图触摸事件监......
  • 百度地图SDK Android版开发 5 地图交互操作
    百度地图SDKAndroid版开发5地图交互操作前言控件控件介绍接口MapViewBaiduMapUiSettingsLogoPosition示例代码Logo指南针比例尺缩放按钮地图上控件边距效果图地图手势地图手势方法说明地图手势开关接口地图手势事件地图单击事件监听接口地图双击事件监听接口地图长......
  • Android面试中Service夺命六小问
    1、请解释一下Android中的Service以及它的用途。Service是Android中的四大组件之一,它可以在后台执行长时间运行的操作,而不需要与用户进行交互。它主要用于执行一些不需要用户界面的任务,例如播放音乐、下载文件、同步数据等。Service有以下几个特点:后台运行:Service......
  • Android笔试面试题AI答之Kotlin(6)
    文章目录24.以下代码执行的结果是什么?25.解释一下下述Kotlin代码有什么问题?26.如何在Kotlin中创建常量?示例注意事项总结27.Koltin可以互换使用IntArray和Kotlin中的Array吗?IntArrayArray<Int>互换使用从IntArray到Array<Int>从Array<Int>到IntArra......
  • Android SDK 遇到的坑之讯飞语音合成
    目录一、讯飞在线语音合成二、组件未安装.(错误码:21002)2.1Android10错误信息2.2Android13错误信息三、错误分析    3.1没有使用SpeechUtility.createUtility()设置appid  3.2 没有将libmsc.so放到工程中往期推荐一、讯飞在线语音合成   ......
  • Android10.0 最近任务Recents功能分析
    在Android10.0上,Recents功能分布在SystemUI和Launcher3里面集成.一.初始化跟Android8.1类似,先看初始化入口:1.Recents.javaRecents继承SystemUI,进程启动后会在Dependency里面通过@Inject进行初始化,然后在SystemUIService里面调用SystemUIApplication的startServicesIfNee......
  • Android HWUI
    AndroidHWUI(HardwareAcceleratedRenderingEngineforUI)是Android系统中用于处理UI渲染的硬件加速引擎。它的主要作用是利用GPU(图形处理单元)来加速UI的渲染过程,从而提高渲染效率和流畅度。以下是AndroidHWUI工作的主要方式和步骤: 一、基本工作原理传统软件的UI绘制是依靠......