Android线性布局和相对布局介绍和应用

一.线性布局

1.线性布局是Android中较为常用的布局方式,它使用标签表示。
2.线性布局有两种方式指定控件位置,一种是水平方向,一种是竖直方向。
3.水平布局或垂直布局
LinearLayout视图是否按行或列来放置它的子视图由LinearLayout视图的android:orientation属性值来决定。
android:orientation=“vertical” 表示是垂直布局
LinearLayout视图是垂直布局时,子视图按行的顺序从上向下排列(即每行上只有一个子视图),每个子视图根据自己的android: layout_height属性值去占用列中的剩余的空间。
android:orientation=“horizontal” 表示是水平布局
LinearLayout视图是水平布局时,子视图按列的顺序从左向右排列,每个子视图根据自己的android: layout_width属性值去占用行中的剩余的空间。

应用实例

在这里插入图片描述

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="red"
            android:layout_weight="1"
            android:background="#FF0000"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="green"
            android:layout_weight="1"
            android:background="#00FF00"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="blue"
            android:layout_weight="1"
            android:background="#0000FF" />
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="yellow"
            android:layout_weight="1"
            android:background="#FFFF00"/>/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="red"
            android:layout_weight="1"
            android:background="#FF0000"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="green"
            android:layout_weight="1"
            android:background="#00FF00"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="blue"
            android:layout_weight="1"
            android:background="#0000FF"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="yellow"
            android:layout_weight="1"
            android:background="#FFFF00"/>
    </LinearLayout>
</LinearLayout>

二.相对布局

1.相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。
2.相对布局常用属性:
(1)相对父布局的位置:
android:layout_alignParentLeft 是否跟父布局左对齐
android:layout_alignParentTop 是否跟父布局顶部对齐
android:layout_alignParentRight 是否跟父布局右对齐
android:layout_alignParentBottom 是否跟父布局底部对齐

android:layout_centerVertical 在父布局中垂直居中
android:layout_centerHorizontal 在父布局中水平居中
android:layout_centerInParent 在父布局中居中
(2)相对于其他控件的位置:
android:layout_toRightOf 在指定控件的右边
android:layout_toLeftOf 在指定控件的左边
android:layout_above 在指定控件的上边
android:layout_below 在指定控件的下边
android:layout_alignBaseline 跟指定控件水平对齐
android:layout_alignLeft 跟指定控件左对齐
android:layout_alignRight 跟指定控件右对齐
android:layout_alignTop 跟指定控件顶部对齐
android:layout_alignBottom 跟指定控件底部对齐

应用实例

在这里插入图片描述

布局代码:

<?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">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:id="@+id/btn2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:layout_toLeftOf="@id/btn2"/>
    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button3"
    android:layout_toRightOf="@id/btn2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="182   27"
        android:id="@+id/btn4"
        android:layout_below="@id/btn2"/>
    <Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Button6"
    android:id="@+id/btn6"
    android:layout_below="@id/btn4"
    android:layout_alignRight="@id/btn2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Button5"
        android:layout_below="@id/btn4"
        android:layout_toLeftOf="@id/btn6"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Button7"
        android:layout_below="@id/btn4"
        android:layout_toRightOf="@id/btn6"/>
</RelativeLayout>
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LongTermism

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

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

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

打赏作者

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

抵扣说明:

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

余额充值