布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,从而编写出精美的界面。

所包含的控件在线性方向上依次排列

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <Button
  6. android:id="@+id/button1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_gravity="top"
  10. android:text="Button 1" />
  11. <Button
  12. android:id="@+id/button2"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_gravity="center_vertical"
  16. android:text="Button 2" />
  17. <Button
  18. android:id="@+id/button3"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="bottom"
  22. android:text="Button 3" />
  23. </LinearLayout>

android:orientation:排列方向,horizontal(水平排列)、vertical(纵向排列)。

android:layout_gravity:指定控件在布局中的对齐方式。

android:layout_weight:用比例的方式来指定控件的大小。

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <Button
  5. android:id="@+id/button1"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_alignParentLeft="true"
  9. android:layout_alignParentTop="true"
  10. android:text="Button 1" />
  11. <Button
  12. android:id="@+id/button2"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentRight="true"
  16. android:layout_alignParentTop="true"
  17. android:text="Button 2" />
  18. <Button
  19. android:id="@+id/button3"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_centerInParent="true"
  23. android:text="Button 3" />
  24. <Button
  25. android:id="@+id/button4"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_alignParentBottom="true"
  29. android:layout_alignParentLeft="true"
  30. android:text="Button 4" />
  31. <Button
  32. android:id="@+id/button5"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentBottom="true"
  36. android:layout_alignParentRight="true"
  37. android:text="Button 5" />
  38. </RelativeLayout>

android:layout_alignParentTop:与父布局的顶边对齐

android:layout_alignParentLeft:与父布局的左边对齐

android:layout_alignParentRight:与父布局的右边对齐

android:layout_alignParentBottom:与父布局的底边对齐

android:layout_centerInParent:与父布局的中间对齐

android:layout_above=”@id/button1″ :让这个控件位于另一个控件(button1)的上方

android:layout_below=”@id/button1″ :让这个控件位于另一个控件(button1)的下方

android:layout_toLeftOf=”@id/button1“:让这个控件位于另一个控件(button1)的左侧

android:layout_toRightOf=”@id/button1″:让这个控件位于另一个控件(button1)的右侧

android:layout_alignLeft=”@id/button1″:让这个控件的左边缘和另一个控件(button1)的左边缘对齐

android:layout_alignRight=”@id/button1″:让这个控件的右边缘和另一个控件(button1)的右边缘对齐

android:layout_alignTop=”@id/button1″:让这个控件的上边缘和另一个控件(button1)的上边缘对齐

android:layout_alignBottom=”@id/button1″:让这个控件的下边缘和另一个控件(button1)的下边缘对齐

这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角。

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <TextView
  5. android:id="@+id/text_view"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:text="This is TextView"
  9. />
  10. <ImageView
  11. android:id="@+id/image_view"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:src="@mipmap/ic_launcher"
  15. />
  16. </FrameLayout>

文字和图片都是位于布局的左上角。由于ImageView是在TextView之后添加的,因此图片压在了文字的上面。

版权声明:本文为tmbto原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tmbto/p/15178049.html