Activities描述UI,处理人和屏幕的交互

布局管理器

  • 相当于是css的flex、grid盒子的容器

线性布局(LinearLayout)

常用属性

  1. android:id
  2. android:layout_width
    1. 可选值:
      1. wrap_content:内容多少宽度多少
      2. match_content:匹配父空间,父空间多少多少
  3. android:layout_height
  4. android:layout_margin
  5. android:layout_padding
  6. android:background
  7. android:orientation
    1. 方向:
      1. vertical:垂直
  8. android:gravity
    1. 在容器中的位置,类似flex布局的align-item,just-content
      1. center_horizontal:水平居中
      2. center_horizontal:垂直居中
      3. center:水平垂直居中
  9. layout_weight:权重

    1. 把剩余的内容按照权重分配,如果先占了50dp,则按照剩下100%-50dp再按照权重分配

      小demo

      ```xml <?xml version=”1.0” encoding=”utf-8”?>

  1. ![image.png](https://cdn.nlark.com/yuque/0/2019/png/432716/1573484374203-302884bb-e8b9-4459-8d3e-343dd6e74f6f.png#align=left&display=inline&height=351&name=image.png&originHeight=702&originWidth=423&size=8796&status=done&width=211.5)
  2. ---
  3. <a name="eGxA5"></a>
  4. ### 相对布局(RelativeLayout)
  5. <a name="MEU0t"></a>
  6. #### 常用属性
  7. 1. `android:layout_toLeftOf`
  8. 1. 在参照物的左边
  9. 2. `android:layout_toRightOf`
  10. 2. `android:layout_alginBottom`
  11. 1. 和参照物底部对齐
  12. 4. `android:layout_alginParentBottom`
  13. 1. 和父控件底部对齐
  14. 5. `android:layout_below`
  15. 1. 在参照物下边
  16. <a name="YNoWa"></a>
  17. #### 小demo
  18. ```xml
  19. <?xml version="1.0" encoding="utf-8"?>
  20. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android:background="#10aabb">
  24. <View
  25. android:id="@+id/view_0"
  26. android:layout_width="100dp"
  27. android:layout_height="100dp"
  28. android:layout_alignParentBottom="true"
  29. android:background="#aabbcc"
  30. />
  31. <View
  32. android:id="@+id/view_1"
  33. android:layout_width="100dp"
  34. android:layout_height="100dp"
  35. android:background="#202020"
  36. />
  37. <View
  38. android:id="@+id/view_2"
  39. android:layout_width="100dp"
  40. android:layout_height="100dp"
  41. android:background="#33ff11"
  42. android:layout_toRightOf="@id/view_1"
  43. />
  44. </RelativeLayout>

image.png


常用控件

View

View是所有控件的父类,所有的控件都继承View的属性