布局

1.线性布局

   垂直或者水平方向上依次摆放控件。

   LinearLayout 的常见属性:

     orientation:对齐方式。

     gravity:设置布局管理器里组件的显示位置。(用layout_gravity没效果,在LinearLayout中只能用gravity属性)

     注意:水平布局中,子组件的layout_width的值不能为”match_parent”,这样只能显示一个组件。垂直布局同理。

   子组件属性:

      layout_gravity:设置子组件在线性布局中的位置。

      layout_weight:设置子组件的权重。

      gravity与layout_gravity的区别:https://www.cnblogs.com/fuck1/p/5461952.html

      注意:LinearLayout中的orientation的属性值会影响子组件的layout_gravity属性,比如,垂直排列时候,layout_gravity=”buttom”没有效果。水平排列一样,layout_gravity=”right”也没有效果。

2.相对布局:

 通过相对定位的方式来控制组件的摆放位置。

 RelativeLayout的属性:

  gravity:各个子组件的对其方式。

  ignoreGravity:指定某个子组件不受gravity属性的影响。

 子组件属性:

    根据父容器来定位:(想位于哪,哪个属性就设置为true)

       左对齐:android:layout_alighParentLeft
       右对齐:android:layout_alighParentRight
       顶端对齐:android:layout_alighParentTop  
       底部对齐:android:layout_alighParentBottom
       水平居中:android:layout_centerHorizontal
       垂直居中:android:layout_centerVertical
       中央位置:android:layout_centerInParent

 

 

 

根据兄弟组件来定位(右面的属性值为兄弟组件的id)

       左边:android:layout_toLeftOf
       右边:android:layout_toRightOf
       上方:android:layout_above
       下方:android:layout_below
        对齐上边界:android:layout_alignTop
        对齐下边界:android:layout_alignBottom
        对齐左边界:android:layout_alignLeft
        对齐右边界:android:layout_alignRight

根据兄弟组件来定位(右面的属性值为数值dp,px)

        android:layout_marginBottom 离某元素底边缘的距离

        android:layout_marginLeft 离某元素左边缘的距离

        android:layout_marginRight 离某元素右边缘的距离

        android:layout_marginTop 离某元素上边缘的距离
 

3.帧布局:

  控件之间会覆蓋。在XML中,后面的控件会覆蓋前面的控件。

  FragmentLayout属性:

     foreground:前景图像。

     foregroundGravity:前景图像的位置。

view的公共属性:

Margin:设置组件与父容器(通常是布局)的边距

android:layout_margin: 指定控件的四周的外部留出一定的边距

android:layout_marginLeft: 指定控件的左边的外部留出一定的边距

android:layout_marginTop: 指定控件的上边的外部留出一定的边距

android:layout_marginRight: 指定控件的右边的外部留出一定的边距

android:layout_marginBottom: 指定控件的下边的外部留出一定的边距

Padding:设置组件内部元素间的边距(可以理解为填充)

android:padding :指定控件的四周的内部留出一定的边距

android:paddingLeft: 指定控件的左边的内部留出一定的边距

android:paddingTop: 指定控件的上边的内部留出一定的边距

android:paddingRight: 指定控件的右边的内部留出一定的边距

android:paddingBottom: 指定控件的下边的内部留出一定的边距
 

点赞