安卓五大布局

安卓五大布局

一、LinearLayout线性布局

重点在于weight的使用

  1. 将宽度或者高度设置为0px

  2. 添加android:layout_weight=”1” 注:1为权重比例

    <LinearLayout xmls:android="http://schems.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="math_parent"
    android:orientation="horizontal">
    <Button
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮1"
    />
    <Button
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮2"
    />
    <Button
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮3"
    />
    </LinearLayout>
    

    此处每个Button设置的weight为1,所占比例的自己的weight/总共的weight

二、RelativeLayout相对布局

概念:相对于id的布局,如果没有相对于id,则默认的参照物为父窗体

1、参照物为父控件:

1.水平居中属性:

android:layout_centerHorizontal="true"

2.垂直居中属性:

android:layout_centerVertical="true"

3.相对于父控件居中:在父空间内水平和垂直一起居中

android:layout_centerInParent="true"

4.相对于父控件的边缘位置:靠近上边缘/下边缘/左边缘/右边缘:

android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
2、参照物为id控件:

1.位于id控件的上方:

android:layout_above="@id/btn1"

2.位于id控件的下方:

android:layout_blow="@id/btn1"

3.位于id控件的左方:

android:layout_toLeftOf="@id/btn1"

4.位于id控件的右方:

android:layout_toRightOf="@id/btn1"

三、FrameLayout帧布局

类似于图层,从上往下依次在实际效果中为从底部往上,例如视频暂停的时候显示在视频上方的暂停图标

<FrameLayout xmls:android="http://schems.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="math_parent">
<TextView
android:layout_width="300px"
android:layout_height="300px"
android:layout_gravity="center"//位于中间,此处待补充,思考:于Layout_centerInParent的区别
/>
</FrameLayout>

四、TableLayout表格布局

TableRow表示第一行,里面的空间从上往下在实际效果中就是从左边第一列开始

<TableLayout xmls:android="http://schems.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="math_parent">
<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <button 
        android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
    <button 
        android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
    <button 
        android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>

五、AbsoluteLayout绝对布局

此布局已经被废弃,不推荐使用,多用于指定屏幕的设备,比如为一个屏幕为2k的屏幕定制机顶盒

    layout_x="20px"//相对于x坐标的像素点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值