二,项目实验——Android 约束布局

目录链接
一,项目实验——Android 线性布局http://t.csdnimg.cn/yDK2c
二,项目实验——Android 约束布局http://t.csdnimg.cn/TS73B
三,个性化定制App登录界面http://t.csdnimg.cn/x7q7D
四,项目实验——Activity与Intenthttp://t.csdnimg.cn/BlTbs

一. 简答题(共1题,100分)

  • (简答题)
    (1)项目目标:掌握约束布局的使用方法及应用。
    (2)项目任务:使用约束布局完成UI界面的设计。
    (3)任务内容:

  • 任务:使用ConstraintLayout完成一个“注册个人信息”的布局。

  • 创建项目ConstraintLayoutDemo,包名:cn.edu.baiyunu.constraintlayoutdemo,Language:
    Java 。

  • 组件树如下图所示:
    在这里插入图片描述

  • 运行效果图:

在这里插入图片描述

实验2 运行效果图

实验素材下载↓

在这里插入图片描述

<?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"
    android:background="@drawable/img2"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="40dp"
        android:text="Registration"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="70dp"
        app:layout_constraintTop_toBottomOf="@+id/title"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <EditText
        android:id="@+id/input_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Please input your name"
        android:layout_marginRight="20dp"
        android:layout_marginTop="55dp"
        android:layout_marginLeft="10dp"
        app:layout_constraintLeft_toRightOf="@+id/name"
        app:layout_constraintTop_toBottomOf="@id/title"
        app:layout_constraintRight_toRightOf="parent"
        />
    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"
        android:text="ID"
        app:layout_constraintTop_toBottomOf="@+id/name"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <EditText
        android:id="@+id/input_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:hint="Please input your ID"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        app:layout_constraintLeft_toRightOf="@+id/id"
        app:layout_constraintTop_toBottomOf="@id/input_name"
        app:layout_constraintRight_toRightOf="parent"  />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender"
        android:textSize="15dp"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="20dp"
        app:layout_constraintTop_toBottomOf="@+id/id"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="-180dp"
        android:layout_marginTop="50dp"
        app:layout_constraintTop_toBottomOf="@id/input_id"
        app:layout_constraintLeft_toRightOf="@id/textView"
        app:layout_constraintRight_toRightOf="parent">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Male"
            android:textSize="15dp"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:textSize="15dp"/>
    </RadioGroup>

    <Button
        android:layout_marginBottom="30dp"
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="115dp"
        android:text="Submit"
        app:layout_constraintRight_toLeftOf="@+id/btn2"
        app:layout_constraintBottom_toBottomOf="parent"/>
    <Button
        android:layout_marginBottom="30dp"
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="115dp"
        android:text="Reset"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <Button
        android:id="@+id/btn3"
        android:layout_marginBottom="30dp"
        android:layout_width="wrap_content"
        android:width="115dp"
        android:layout_height="wrap_content"
        android:text="Exit"
        app:layout_constraintLeft_toRightOf="@+id/btn2"
        app:layout_constraintBottom_toBottomOf="parent"/>




</androidx.constraintlayout.widget.ConstraintLayout>


在这里插入图片描述

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* [【Android】ConstraintLayout约束布局最全解析](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 ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值