当前位置:   article > 正文

Android中ConstraintLayout布局的基本用法_android constraintlayout 设置 radius

android constraintlayout 设置 radius

约束布局(ConstraintLayout)是目前谷歌推荐的一种布局,它的功能强大。相对于LinearLayout和RelationLayout相比,它的用法更加灵活,同时代码量更小。下面介绍一下约束布局的简单用法。

例子1:最简单用法

  1. app:layout_constraintStart_toStartOf="id"//该模块的左边和id模块的左边对齐
  2. app:layout_constraintEnd_toStartOf="id"//该模块的右边和id模块的左边对齐
  3. app:layout_constraintStart_toEndOf="id"//该模块的左边和id模块的右边对齐
  4. app:layout_constraintEnd_toEndOf="id"//该模块的右边和id模块的右边对齐
  5. app:layout_constraintTop_toTopOf="id"//该模块的上边和id模块的上边对齐
  6. app:layout_constraintTop_toBottomOf="id"//该模块的上边和id模块的底边对齐
  7. app:layout_constraintBottom_toTopOf="id"//该模块的底边和id模块的上边对齐
  8. app:layout_constraintBottom_toBottomOf="id"//该模块的底边和id模块的底边对齐

前四条是横向的约束条件,后四条是纵向的约束条件,一个模块必须同时要有横向和纵向的约束条件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Hello World!"
  12. android:textSize="20dp"
  13. app:layout_constraintStart_toStartOf="parent" //模块的左边和父布局的左边对齐
  14. app:layout_constraintTop_toTopOf="parent" /> //模块的顶端和父布局的顶端对齐
  15. </androidx.constraintlayout.widget.ConstraintLayout>

 例子2:两模块相对布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:id="@+id/Text1" //设置ID
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="I'm first"
  13. android:textSize="20dp"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent" />
  16. <TextView
  17. android:id="@+id/Text2"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="I'm second"
  21. android:textSize="20dp"
  22. app:layout_constraintStart_toEndOf="@id/Text1" //Text2模块的左边和Text1模块的右边对齐
  23. app:layout_constraintTop_toBottomOf="@id/Text1" /> //Text2模块的上边和Text1模块的底边对齐
  24. </androidx.constraintlayout.widget.ConstraintLayout>

  例子3:基线对齐

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:id="@+id/Text1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="I'm first"
  13. android:textSize="50dp"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent" />
  16. <TextView
  17. android:id="@+id/Text2"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="I'm second"
  21. android:textSize="30dp"
  22. app:layout_constraintStart_toEndOf="@id/Text1"
  23. app:layout_constraintBaseline_toBaselineOf="@+id/Text1"/> //基线对齐
  24. </androidx.constraintlayout.widget.ConstraintLayout>

  例子4:角度约束

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:id="@+id/Text1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="I'm first"
  13. android:textSize="50dp"
  14. app:layout_constraintStart_toStartOf="parent" //左边与父布局左边对齐,右边与父布局右边对齐
  15. app:layout_constraintEnd_toEndOf="parent" //那么它就在中间
  16. app:layout_constraintTop_toTopOf="parent" />
  17. <TextView
  18. app:layout_constraintCircle="@id/Text1" //参考的模块
  19. android:id="@+id/Text2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="I'm second"
  23. android:textSize="30dp"
  24. app:layout_constraintCircleAngle="120" //距离
  25. app:layout_constraintCircleRadius="120dp" //角度
  26. tools:ignore="MissingConstraints" />
  27. </androidx.constraintlayout.widget.ConstraintLayout>

 这是ContriantLayout布局的基本用法,这些看着简单,但灵活运用起来并不是那么容易。有了约束布局,让页面设计更加灵活。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/225597
推荐阅读
相关标签
  

闽ICP备14008679号