Android的常用布局

一 布局文件的创建
1.点左面layout文件夹,单击鼠标右键,选New—>layout resource file
在这里插入图片描述
在这里插入图片描述
第二种方式
点左面layout文件夹,单击鼠标右键,选New—>xml-use-选layout xml file/values xml file
在这里插入图片描述
二 布局文件类型
1.表格布局(TableLayout)
以表格形式排列控件,通过行和列页面分为多个单元格,每个单元格都可以添加控件。
表格布局需要和TableRow配合使用,每一行都由TableRow对象组成,因为TableRow的数量决定表格的行数,列数由最多控件的TableRow决定。

布局属性功能
android:stretchColumns该列被拉伸
android:shrinkColumns该列被收缩
android:collapseColumns该列被隐藏
控件属性功能
android:layout_column设置单元格显示位置下标值从0开始的
android:layout_span设置单元格占几列,默认1列

例子:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:stretchColumns="2"
    android:layout_height="match_parent">

    <TableRow
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button5"
            android:layout_column="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </TableRow>
</TableLayout>

效果:
在这里插入图片描述
2.线性布局(LinearLayout)
特点:以水平或或垂直反向排列,水平排列时,从左到右;垂直排列时,从上到下,控件的方向用orientation控制,默认用android:orientation="horizontal",是水平排列,vertical垂直排列

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:textSize="20sp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:textSize="20sp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:textSize="20sp"
        />
</LinearLayout>

效果图
在这里插入图片描述
注意
这里的layout_width的属性值不能设置成match_parent(填充父窗体),其余的控件会被挤出来,显示不出来,layout_width设置成wrap_content(包裹内容)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="match_parent"
        
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:textSize="20sp"
        />
    <!--这里的layout_width的属性值不能设置成match_parent(填充父窗体),
    其余的控件会被挤出来,显示不出来-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:textSize="20sp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:textSize="20sp"
        />
</LinearLayout>

这样的效果是
在这里插入图片描述
在上面三个按钮的内个图,可以发现按钮并没有把行填满,在真实的情况下,并没有这样的,影响美观。
这里可以用layout_weight设置权重,在button中使用属性时,控件宽度不在由layout_width决定,指定为0dp就可以了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:textSize="20sp"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:textSize="20sp"
        android:layout_weight="2"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:textSize="20sp"
        android:layout_weight="1"
        />
</LinearLayout>

效果为:
在这里插入图片描述
3.相对布局(RelativeLayout)
相对布局是通过相对定位的方式指定控件位置,即以其他控件或父容器为参照物,来摆放控件位置。

控件属性名称功能
android:layout_centerInParent当前控件位于父布局的中央位置
android:centerVertical当前控件位于父布局垂直居中位置
android:layout_centerHorizontal当前控件位于父控件水平居中位置
android:layout_above当前控件位于某控件上方
android:layout_below当前控件位于某控件下方
android:layout_toLeftOf当前控件位于某控件左侧
android:layout_toRightOf当前控件位于某控件右侧
android:layout_alignParentTop当前控件位于布局顶端
android:layout_alignParentBottom当前控件位于布局底端
android:layout_alignParentLeft当前控件位于布局左侧
android:layout_alignParentRight当前控件位于布局右侧
android:layout_alignTop当前控件上边界与某控件上边界对齐
android:layout_alignBottom当前控件下边界与某控件下边界对齐
android:layout_alignLeft当前控件左边界与某控件左边界对齐
android:layout_alignRight当前控件右边界与某控件右边界对齐
android:layout_marginTop当前控件上边界与某控件的距离
android:layout_marginBottom当前控件下边界与某控件的距离
android:layout_marginLeft当前控件左边界与某控件的距离
android:layout_marginRight当前控件右边界与某控件的距离
布局控件属性功能描述
android:paddingTop设置布局顶部内边距的距离
android:paddingBottom设置布局底部内边距的距离
android:paddingLeft设置布局左边内边距的距离
android:paddingRight设置布局右边内边距的距离
android:padding设置布局四周内边距的距离

例子

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="20sp"
    >
    <!--
     android:paddingBottom="20sp"父窗体距底边距20sp
    -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="按钮1"
        android:textSize="20dp"
        android:layout_alignParentBottom="true"
        />
    <!--
     android:layout_alignParentBottom属性共2个值,如果位true代表该控件的底部与其父控件的底部对齐
    -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="按钮2"
        android:textSize="20dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="200sp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn3"
        android:text="按钮3"
        android:textSize="20dp"
        android:layout_toRightOf="@id/btn2"
        android:layout_alignBottom="@id/btn2"
        android:layout_marginBottom="100sp"
        />
    <!--
    @id/btn2代表引用id等于btn2的控件
    -->
</RelativeLayout>

效果:
在这里插入图片描述
(4) 帧布局(FrameLayout)
该布局为每个加入其中的控件创建一个空白区域,称为一帧,每个控件占据一帧,采用帧布局方式设计界面时,所有控件都默认显示在屏幕左上角,并按照先后放入的顺序重叠摆放,先放入的控件显示在最底层,后放入的控件显示在最顶层。帧布局适用于图层设计。

布局属性功能
android:foreground设置帧布局容器的前景图像
android:foregroundGravity设置前景图像显示位置
注意:android:foreground设置帧布局容器的前景图片,始终在所有控件之上
    android:foregroundGravity设置前景图像显示位置
    @mipmap/ic_launcher代表引入mipmap文件下的ic_launcher.png图片

例子

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@mipmap/ic_launcher"
    android:foregroundGravity="left"
    >

    <Button
        android:layout_height="400dp"
        android:layout_width="400dp"
        android:text="按钮1"
        android:textSize="20dp"
        ></Button>
    <Button
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:text="按钮1"
        android:textSize="20dp"
        ></Button>
</FrameLayout>

效果
在这里插入图片描述
常用的就是这四种

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值