Android约束布局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/text1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:text="1"
        android:textSize="20dp"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"
        android:text="2"
        android:textSize="20dp"
        app:layout_constraintLeft_toRightOf="@+id/text1" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_light"
        android:text="3"
        android:textSize="20dp"
        app:layout_constraintTop_toBottomOf="@+id/text1" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_light"
        android:text="4"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toTopOf="@id/text4"
        app:layout_constraintLeft_toRightOf="@id/text2" />
</android.support.constraint.ConstraintLayout>


运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android约束布局(ConstraintLayout)是一种可以灵活控制子控件位置和大小的布局方式。它是Android官方在2016年Google的I/O大会推出的,并且在最新版的Android Studio中成为创建布局文件的默认根元素。\[1\] 在约束布局中,子控件的位置和大小是通过设置约束条件来实现的。例如,可以使用app:layout_constraintLeft_toLeftOf和app:layout_constraintTop_toTopOf属性来指定子控件相对于父布局的左边和顶部的位置。\[2\] 此外,约束布局还支持设置子控件的最小宽度(android:minWidth)、最小高度(android:minHeight)、最大宽度(android:maxWidth)和最大高度(android:maxHeight)。为了替代match_parent属性,官方推荐使用0dp(MATCH_CONSTRAINT)并结合约束条件来设置子控件的宽度和高度。\[3\] 下面是一个使用约束布局的示例代码: ```xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView android:id="@+id/TextView1" android:layout_width="0dp" android:layout_height="wrap_content" android:background="#E8C99B" android:gravity="center" android:text="textview1" android:textColor="@color/black" android:textSize="25sp" android:textStyle="bold" android:layout_marginLeft="100dp" android:layout_marginTop="100dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> ``` 这个示例中,TextView的宽度被设置为0dp(MATCH_CONSTRAINT),并且通过约束条件app:layout_constraintLeft_toLeftOf和app:layout_constraintTop_toTopOf来确定其位置。同时,还设置了最小宽度、最小高度、背景颜色等属性。 #### 引用[.reference_title] - *1* [【AndroidConstraintLayout约束布局最全解析](https://blog.csdn.net/huweiliyi/article/details/122894823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Android——ConstraintLayout(约束布局)](https://blog.csdn.net/The_onion/article/details/127675500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值