横向、纵向充满屏幕(4个按钮正好各占1/4屏幕面积(居中))——田字格

前端之家收集整理的这篇文章主要介绍了横向、纵向充满屏幕(4个按钮正好各占1/4屏幕面积(居中))——田字格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

田字格 amxl

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@color/main_bg">
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:layout_weight="50"
  10. android:orientation="vertical">
  11. <Button
  12. android:text="Button1"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:layout_weight="50"
  16. android:id="@+id/button1" />
  17. <Button
  18. android:text="Button2"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:layout_weight="50"
  22. android:id="@+id/button2" />
  23. </LinearLayout>
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="match_parent"
  27. android:layout_weight="50"
  28. android:orientation="vertical">
  29. <Button
  30. android:text="Button3"
  31. android:layout_width="match_parent"
  32. android:layout_height="match_parent"
  33. android:layout_weight="50"
  34. android:id="@+id/button3" />
  35. <Button
  36. android:text="Button4"
  37. android:layout_width="match_parent"
  38. android:layout_height="match_parent"
  39. android:layout_weight="50"
  40. android:id="@+id/button4" />
  41. </LinearLayout>
  42. </LinearLayout>

android:layout_weight="50" 权重属性

注意权重属性就行啦,比如在LinearLayout 内一个权重是 70 一个权重是30 那么权重70的比30的宽按比例宽的

效果图:


使用权重设置分屏占用比例如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:minWidth="25px"
  7. android:minHeight="25px">
  8. <FrameLayout
  9. android:minWidth="25px"
  10. android:minHeight="25px"
  11. android:layout_weight="1"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:id="@+id/FrameLayout1">
  15. <LinearLayout
  16. android:orientation="horizontal"
  17. android:minWidth="25px"
  18. android:minHeight="25px"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:id="@+id/linearLayout2">
  22. <Button
  23. android:text="Button1"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:id="@+id/button1" />
  27. </LinearLayout>
  28. </FrameLayout>
  29. <FrameLayout
  30. android:minWidth="25px"
  31. android:minHeight="25px"
  32. android:layout_weight="8"
  33. android:layout_width="match_parent"
  34. android:layout_height="match_parent"
  35. android:id="@+id/FrameLayout2">
  36. <LinearLayout
  37. android:id="@+id/LinearLayout2"
  38. android:layout_width="match_parent"
  39. android:layout_height="match_parent"
  40. android:orientation="horizontal"
  41. android:layout_alignParentBottom="true">
  42. <Button
  43. android:id="@+id/Button2"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:layout_gravity="center_vertical"
  47. android:text="Button2"
  48. android:background="#00ffff"
  49. android:layout_marginRight="1dp"
  50. android:textColor="#000000"
  51. android:layout_weight="1"
  52. android:textSize="50px" />
  53. <Button
  54. android:id="@+id/Button3"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:layout_gravity="center_vertical"
  58. android:text="Button3"
  59. android:background="#00ffff"
  60. android:layout_weight="1"
  61. android:layout_marginLeft="0.0dp"
  62. android:textColor="#000000"
  63. android:textSize="50px" />
  64. </LinearLayout>
  65. </FrameLayout>
  66. </LinearLayout>

猜你在找的XML相关文章