Android的五种布局

LinearLayout 线性布局

  首先我们先认识一下长宽的定义方式:wrap_content 和match_parent (fill_parent)。
wrap_content:根据内容调整长或者宽。
match_parent: 自定填充,填满定义的长或者宽。

布局属性:

(1)android:orientation:
  定义线性布局的排列方式,有两个选项:vertical和horizontal。vertical代表垂直分布,horizontal代表水平分布。默认是horizontal。
这里写图片描述 这里写图片描述
      vertical          horizontal

(2)android:layout_gravity
  定义线性布局中控件的布局方式,有很多选项:top, bottom, left , right, center_vertical, fill_vertical, center_horizontal, fill_horizontal, center, fill , clip_vertical, clip_horizontal , start, end。看英文意思就可以理解排列不布局方式,在此不在赘述。

(3)android:layout_weight
  定义线性布局中通过比例来控制控件的大小。例如:
定义两个按钮,分别占比重1/3和2/3。

    <Button
        android:layout_width="wrap_content"
        android:layout_height="0" 
        android:layout_weight="1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0"
        android:layout_weight="1"/>

            这里写图片描述
            
            
  定义两个按钮, 分别占比重1/5和2/5。需要在开始 “LinearLayout ”中定义一句:”android:weightSum=”5”“,将布局分为5等分。
  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0" 
        android:layout_weight="1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0"
        android:layout_weight="1"/>
</LinearLayout>

            这里写图片描述
            
(3)android:layout_marginXXX  
  XXX可代表:Bottom,End,Left, Right, Start, Top。分别是距离后面id控件的底部,结束,左面,右面,开始,顶部的距离。(注意距离一般用单位dp表示。)

          这里写图片描述

RelativeLayout 相对布局

布局属性:

(1)android:layout_alignParentXXX
  XXX分别代表:Left Right Bottom Top Start End, 分别代表与父空间的左, 右, 低, 顶, 开始, 结尾对齐。
  
         这里写图片描述

(2)android:layout_centerXXX         
  XXX是指:InParent , Vertical, Horizotial。是相对于父空间的中间的布局。
(3)android:layout_toXXXof和android:layout_above(below) 
  XXX是指:LeftOf,Right。是相对于后面id控件的左右。
  above和below是相对于后面id控件的上下。
(4)android:layout_alginBaseLine   
  基准线对齐。

FrameLayout帧布局

  这种布局使用比较少见。
  是一层一层覆盖的。举例理解:放置了一个和一个文本输入框,可以看到他们是重叠的。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</FrameLayout>

   这里写图片描述

布局属性:

android:visibility
  属性值有三个分别是:visible(可见)、 invisible(不可见)、 gone(去除)


TableLayout 表布局

  用表格的形式来布局。
  在布局中一定会包含<TableRow> </TableRow>。一般很少用到,可能就是登陆界面会使用。

布局属性:

(1)android:stretchColumns=”0” 
  本属性的含义是将表格剩余空间用第一列表格填充完整。“0”代指第一列,当然属性值也可以是“0,1,2”,该属性值得含义是让表格的第1,2,3列共同填充剩余空间。
(2)android:layout_span=”2”   
  该属性的含义是让调用该属性控件在表格中占两行。如果属性值为“3”,则为占三行。
(3)android:shrinkColumns=”0”    
  该属性含义是将第一列中的文字自动调整,以适应空间大小。


AbsoluteLayout绝对布局

  因为该布局是采用坐标的形式来进行布局定位的,布局的dp都是固定的,以至于在不同分辨率的手机上显示的布局大小不同造成差异,一次Android中很少有采用这种布局方式的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小_爽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值