• Android学习笔记 64. ConstraintLayout使用布局编辑器


    Android学习笔记

    Android基础开发——必备知识

    64. ConstraintLayout使用布局编辑器

    64.1 创建ColorMyViews项目

    在这里插入图片描述

    创建

    直接运行项目

    在这里插入图片描述

    64.2 使用Layout Editor构建一个ConstraintLayout

    在这里插入图片描述

    设计选项卡中,默认“自动连接”是开启状态。

    将其关闭。

    修改默认边距

    在这里插入图片描述

    修改后就不用每次添加约束都添加边距了。

    官方代码地址:

    https://github.com/google-developer-training/android-kotlin-fundamentals-apps

    64.3 项目代码

    MainActivity.kt

    package com.dingjiaxiong.colormyviews
    
    import android.graphics.Color
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    import android.widget.Button
    import android.widget.TextView
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            setListeners()
        }
    
        /**
         * Attaches listeners to all the views.
         */
        private fun setListeners() {
    
            val boxOneText = findViewById<TextView>(R.id.box_one_text)
            val boxTwoText = findViewById<TextView>(R.id.box_two_text)
            val boxThreeText = findViewById<TextView>(R.id.box_three_text)
            val boxFourText = findViewById<TextView>(R.id.box_four_text)
            val boxFiveText = findViewById<TextView>(R.id.box_five_text)
    
            val rootConstraintLayout = findViewById<View>(R.id.constraint_layout)
    
            val redButton = findViewById<Button>(R.id.red_button)
            val greenButton = findViewById<Button>(R.id.green_button)
            val yellowButton = findViewById<Button>(R.id.yellow_button)
    
            val clickableViews: List<View> =
                listOf(
                    boxOneText, boxTwoText, boxThreeText,
                    boxFourText, boxFiveText, rootConstraintLayout,
                    redButton, greenButton, yellowButton
                )
    
            for (item in clickableViews) {
                item.setOnClickListener { makeColored(it) }
            }
        }
    
        /**
         * Sets the background color of a view depending on it's resource id.
         * This is a way of using one click handler to do similar operations on a
         * group of views.
         */
    
        private fun makeColored(view: View) {
    
    
            val box_three_text = findViewById<TextView>(R.id.box_three_text);
    
            val box_four_text = findViewById<TextView>(R.id.box_four_text);
    
            val box_five_text = findViewById<TextView>(R.id.box_five_text);
            when (view.id) {
    
                // Boxes using Color class colors for background
                R.id.box_one_text -> view.setBackgroundColor(Color.DKGRAY)
                R.id.box_two_text -> view.setBackgroundColor(Color.GRAY)
    
                R.id.box_three_text -> view.setBackgroundColor(Color.BLUE)
                R.id.box_four_text -> view.setBackgroundColor(Color.MAGENTA)
                R.id.box_five_text -> view.setBackgroundColor(Color.BLUE)
    
                // Boxes using custom colors for background
                R.id.red_button -> box_three_text.setBackgroundResource(R.color.my_red)
                R.id.yellow_button -> box_four_text.setBackgroundResource(R.color.my_yellow)
                R.id.green_button -> box_five_text.setBackgroundResource(R.color.my_green)
    
                else -> view.setBackgroundColor(Color.LTGRAY)
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    布局

    
    <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:id="@+id/constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/box_one_text"
            style="@style/whiteBox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:layout_marginTop="@dimen/margin_wide"
            android:layout_marginEnd="@dimen/margin_wide"
            android:fontFamily="@font/roboto"
            android:text="@string/box_one"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/box_two_text"
            style="@style/whiteBox"
            android:layout_width="130dp"
            android:layout_height="130dp"
            android:layout_marginStart="@dimen/margin_wide"
            android:layout_marginTop="@dimen/margin_wide"
            android:text="@string/box_two"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/box_one_text" />
    
        <TextView
            android:id="@+id/box_three_text"
            style="@style/whiteBox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:layout_marginEnd="@dimen/margin_wide"
            android:text="@string/box_three"
            app:layout_constraintBottom_toTopOf="@+id/box_four_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/box_two_text"
            app:layout_constraintTop_toTopOf="@+id/box_two_text" />
    
        <TextView
            android:id="@+id/box_four_text"
            style="@style/whiteBox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:layout_marginTop="@dimen/margin_wide"
            android:layout_marginEnd="@dimen/margin_wide"
            android:layout_marginBottom="@dimen/margin_wide"
            android:text="@string/box_four"
            app:layout_constraintBottom_toTopOf="@+id/box_five_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/box_two_text"
            app:layout_constraintTop_toBottomOf="@+id/box_three_text" />
    
        <TextView
            android:id="@+id/box_five_text"
            style="@style/whiteBox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:layout_marginEnd="@dimen/margin_wide"
            android:text="@string/box_five"
            app:layout_constraintBottom_toBottomOf="@+id/box_two_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/box_two_text"
            app:layout_constraintTop_toBottomOf="@+id/box_four_text" />
    
        <TextView
            android:id="@+id/label_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:text="@string/how_to_play"
            android:textSize="@dimen/box_text_size"
            android:textStyle="bold"
            app:layout_constraintBaseline_toBaselineOf="@+id/info_text"
            app:layout_constraintStart_toStartOf="parent" />
    
        <TextView
            android:id="@+id/info_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:layout_marginTop="@dimen/margin_wide"
            android:layout_marginEnd="@dimen/margin_wide"
            android:layout_marginBottom="@dimen/margin_wide"
            android:fontFamily="@font/roboto"
            android:text="@string/tap_the_boxes_and_buttons"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/label_text"
            app:layout_constraintTop_toBottomOf="@+id/box_two_text"
            app:layout_constraintVertical_bias="0.0" />
    
        <Button
            android:id="@+id/red_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:text="@string/button_red"
            android:visibility="visible"
            app:layout_constraintBaseline_toBaselineOf="@+id/yellow_button"
            app:layout_constraintEnd_toStartOf="@+id/yellow_button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent" />
    
        <Button
            android:id="@+id/yellow_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_wide"
            android:layout_marginTop="@dimen/margin_wide"
            android:layout_marginBottom="@dimen/margin_wide"
            android:text="@string/button_yellow"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/green_button"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/red_button"
            app:layout_constraintTop_toBottomOf="@+id/info_text"
            app:layout_constraintVertical_bias="1.0" />
    
        <Button
            android:id="@+id/green_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/margin_wide"
            android:text="@string/button_green"
            app:layout_constraintBaseline_toBaselineOf="@+id/yellow_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/yellow_button" />
    
    
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    64.4 运行

    在这里插入图片描述

  • 相关阅读:
    【无标题】
    基于 iframe 的微前端框架 —— 擎天
    Java基础之浅谈接口
    这些负载均衡都解决哪些问题?服务、网关、NGINX?
    重新理解微服务之它还那么纯粹吗?
    Docker学习整理
    侯捷——2.C++标准库 体系结构与内核分析
    企业微信获取第三方应用凭证
    Redis持久化
    【JUC】多线程基础概述
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126339729