当前位置:   article > 正文

ConstraintLayout使用详解_constraintlayout java版本

constraintlayout java版本

作为一名程序员,可能会经历以下难受的事情:

  1. 解决难以调试的代码错误
  2. 处理复杂的代码库和维护遗留代码
  3. 修改已经存在很长时间的代码,需要考虑兼容性和稳定性
  4. 长时间工作,缺乏身体运动和社交互动,导致压力和孤独感
  5. 遇到不能解决的技术问题,需要进行大量的研究和学习
  6. 调整工作计划和排期,以满足紧急需求和客户需求
  7. 参加无聊和冗长的会议
  8. 处理糟糕的老板或客户的需求和要求
  9. 社交能力不足,与其他团队成员沟通和交流困难
  10. 压力大,需要在短时间内完成复杂的任务。

ConstraintLayout是Android Studio 2.3版本中推出的一种布局,它被认为是一种更加灵活、更加强大的布局,它可以在大多数情况下替代其他布局,比如RelativeLayout、LinearLayout等。下面是ConstraintLayout的使用详解。

  1. 基础概念

约束(Constraint):Constraint指的是控件与父容器或其它控件之间的关系,比如:控件的上边缘与其父容器的上边缘对齐、控件的左边缘与另一个控件的右边缘对齐等。

约束线(Constraint Line):Constraint Line指的是两个控件之间的连接线,每个控件都有四条Constraint Line,分别是上、下、左、右。

  1. 布局实现

在使用ConstraintLayout布局时,需要做的第一件事情就是在build.gradle文件中添加依赖:

  1. dependencies {
  2. implementation 'com.android.support.constraint:constraint-layout:2.0.4'
  3. }

然后,在layout文件中使用ConstraintLayout标签:

  1. <androidx.constraintlayout.widget.ConstraintLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <!-- 子控件 -->
  5. </androidx.constraintlayout.widget.ConstraintLayout>

接下来,我们就可以开始使用约束来布局了。下面是ConstraintLayout的典型布局方式

  1. <Button
  2. android:id="@+id/btn1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:text="Button 1" />
  6. <Button
  7. android:id="@+id/btn2"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="Button 2" />
  11. <Button
  12. android:id="@+id/btn3"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Button 3" />
  16. <Button
  17. android:id="@+id/btn4"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="Button 4" />
  21. <Button
  22. android:id="@+id/btn5"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="Button 5" />
  26. <Button
  27. android:id="@+id/btn6"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="Button 6" />
  31. <Button
  32. android:id="@+id/btn7"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="Button 7" />
  36. <Button
  37. android:id="@+id/btn8"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="Button 8" />
  41. <Button
  42. android:id="@+id/btn9"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="Button 9" />
  46. <Button
  47. android:id="@+id/btn10"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="Button 10" />

效果图

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号