当前位置:   article > 正文

Android Studio开发学习(七)———RelativeLayout(相对布局)_andriod studio4.0.1 relativelayout

andriod studio4.0.1 relativelayout

 在上期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局,更多的时候更钟情于它的 weight(权重) 属性,等比例划分,对屏幕适配还是 帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow; 但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margin +padding就可以设置组件的显示位置,是比较方便!当然,也不是绝对的,具体问题具体分析吧!

一、根据父容器定位

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/main"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <!-- layout_alignParentLeft: 左对齐-->
  7. <androidx.appcompat.widget.AppCompatButton
  8. android:layout_width="120dp"
  9. android:layout_height="100dp"
  10. android:layout_alignParentLeft="true"
  11. android:background="#ff2328"
  12. android:text="1" />
  13. <!-- layout_alignParentRight:右对齐-->
  14. <androidx.appcompat.widget.AppCompatButton
  15. android:layout_width="80dp"
  16. android:layout_height="40dp"
  17. android:layout_alignParentRight="true"
  18. android:background="#aaa72d"
  19. android:text="2" />
  20. <!-- layout_alignParentTop: 顶部对齐-->
  21. <androidx.appcompat.widget.AppCompatButton
  22. android:layout_width="80dp"
  23. android:layout_height="40dp"
  24. android:layout_alignParentTop="true"
  25. android:background="#00aa7d"
  26. android:text="3" />
  27. <!-- layout_alignParentBottom: 底部对齐-->
  28. <androidx.appcompat.widget.AppCompatButton
  29. android:layout_width="80dp"
  30. android:layout_height="40dp"
  31. android:layout_alignParentBottom="true"
  32. android:background="#2eaa0f"
  33. android:text="4" />
  34. <!-- android:layout_centerHorizontal: 水平居中-->
  35. <androidx.appcompat.widget.AppCompatButton
  36. android:layout_width="80dp"
  37. android:layout_height="40dp"
  38. android:layout_centerHorizontal="true"
  39. android:background="#aa7fa9"
  40. android:text="5" />
  41. <!-- andriod:layout_centerVertical: 垂直居中-->
  42. <androidx.appcompat.widget.AppCompatButton
  43. android:layout_width="80dp"
  44. android:layout_height="40dp"
  45. android:layout_centerVertical="true"
  46. android:background="#77b8ff"
  47. android:text="6" />
  48. <!-- android:layout_centerInParent: 中间位置-->
  49. <androidx.appcompat.widget.AppCompatButton
  50. android:layout_width="80dp"
  51. android:layout_height="40dp"
  52. android:layout_centerInParent="true"
  53. android:background="#cc58be"
  54. android:text="7" />
  55. </RelativeLayout>

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

闽ICP备14008679号