当前位置:   article > 正文

Android 实现控件对称布局(约束布局和线性布局)_paddinghorizontal

paddinghorizontal

        画界面时会遇到很多界面上的布局,虽然很简单,但是每次做起来不熟练,总结一下一些日常的

一.实现界面上的两个空间对称布局

方法一、约束布局guideLine.适用于两个控件不确定宽高,且约束条件较多

Guideline是只能用在ConstraintLayout布局里面的一个工具类,用于辅助布局,类似为辅助线,可以设置android:orientation属性来确定是横向的还是纵向的。

重要属性:

  • layout_constraintGuide_begin,指定左侧或顶部的固定距离,如10dp,在距离左侧或者顶部10dp的位置会出现一条辅助线
  • layout_constraintGuide_end,指定右侧或底部的固定距离,如50dp,在距离右侧或底部50dp的位置会出现一条辅助线
  • layout_constraintGuide_percent,指定在父控件中的宽度或高度的百分比,如0.5,表示距离垂直或者水平居中。

 

  1. <androidx.constraintlayout.widget.ConstraintLayout
  2. android:layout_marginTop="20dp"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <androidx.appcompat.widget.AppCompatTextView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. app:layout_constraintStart_toStartOf="parent"
  9. app:layout_constraintEnd_toEndOf="parent"
  10. app:layout_constraintTop_toTopOf="parent"
  11. android:textColor="@color/black"
  12. android:text="guideline 实现对称布局"/>
  13. <androidx.appcompat.widget.AppCompatButton
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="确定"
  17. android:layout_marginTop="30dp"
  18. android:layout_marginEnd="20dp"
  19. app:layout_constraintTop_toTopOf="parent"
  20. app:layout_constraintEnd_toStartOf="@id/guide_line_1"
  21. android:textColor="@color/white"
  22. android:background="@drawable/rgb1a1c1f_r8"
  23. android:textStyle="bold"
  24. android:paddingHorizontal="20dp"/>
  25. <androidx.constraintlayout.widget.Guideline
  26. android:id="@+id/guide_line_1"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:orientation="vertical"
  30. app:layout_constraintGuide_percent="0.5"/>
  31. <androidx.appcompat.widget.AppCompatButton
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text="取消"
  35. android:layout_marginTop="30dp"
  36. android:background="@drawable/rgb1a1c1f_r8"
  37. android:layout_marginStart="20dp"
  38. app:layout_constraintTop_toTopOf="parent"
  39. app:layout_constraintStart_toEndOf="@id/guide_line_1"
  40. android:textColor="@color/white"
  41. android:textStyle="bold"
  42. android:paddingHorizontal="20dp"/>
  43. </androidx.constraintlayout.widget.ConstraintLayout>

方法二:线性布局,都知道线性布局要么水平要么垂直,当要求其子控件均匀排列,或者按一定比例占据父容器的宽高时,可用到其weight属性 

这是实现左右对称

  1. <LinearLayout
  2. android:paddingHorizontal="20dp"
  3. android:layout_marginTop="20dp"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:id="@+id/ll_1"
  7. app:layout_constraintStart_toStartOf="parent"
  8. app:layout_constraintTop_toBottomOf="@id/cl_1"
  9. android:orientation="horizontal">
  10. <androidx.appcompat.widget.AppCompatButton
  11. android:background="@drawable/rgb1a1c1f_r8"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_marginEnd="20dp"
  15. android:text="@string/video"
  16. android:layout_weight="1"/>
  17. <androidx.appcompat.widget.AppCompatButton
  18. android:background="@drawable/rgb1a1c1f_r8"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginStart="20dp"
  22. android:text="游戏"
  23. android:layout_weight="1"/>
  24. </LinearLayout>

这是实现weight比例:

 

 

  1. <LinearLayout
  2. android:paddingHorizontal="10dp"
  3. android:layout_marginTop="20dp"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:id="@+id/ll_1"
  7. app:layout_constraintStart_toStartOf="parent"
  8. app:layout_constraintTop_toBottomOf="@id/cl_1"
  9. android:orientation="horizontal">
  10. <androidx.appcompat.widget.AppCompatButton
  11. android:background="@drawable/rgb1a1c1f_r8"
  12. android:layout_width="0dp"
  13. android:layout_height="wrap_content"
  14. android:layout_marginEnd="10dp"
  15. android:text="@string/video"
  16. android:layout_weight="1"/>
  17. <androidx.appcompat.widget.AppCompatButton
  18. android:background="@drawable/rgb1a1c1f_r8"
  19. android:layout_width="0dp"
  20. android:layout_height="wrap_content"
  21. android:layout_marginEnd="10dp"
  22. android:text="游戏"
  23. android:layout_weight="1"/>
  24. <androidx.appcompat.widget.AppCompatButton
  25. android:background="@drawable/rgb1a1c1f_r8"
  26. android:layout_width="0dp"
  27. android:layout_height="wrap_content"
  28. android:layout_marginStart="10dp"
  29. android:text="小说"
  30. android:layout_weight="4"/>
  31. </LinearLayout>

很基础的知识,做个人总结用。

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

闽ICP备14008679号