Android_studio布局

本文介绍了Android中的五大布局方式,包括线性布局、帧布局、表格布局、相对布局和绝对布局。线性布局是最常用的一种,可通过android:orientation属性设置垂直或水平方向,使用layout_weight属性调整组件相对大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android中常用的5大布局方式有以下几种:

  • 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
  • 帧布局(FrameLayout):组件从屏幕左上方布局组件。
  • 表格布局(TableLayout):按照行列方式布局组件。
  • 相对布局(RelativeLayout):相对其它组件的布局方式。
  •  绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。
1. 线性布局

线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。

常用的属性:

android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小

布局结果如下:


原代码如下:


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.myapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="按钮2" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:layout_weight="3"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="right"
            >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>


2. 帧布局
帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件,只要设置好他们的位置即可。
结果如下:
原代码如下:
<?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">

    <ImageView
        android:layout_width="380dp"
        android:layout_height="380dp"
        android:background="#ff00ff"
        android:layout_gravity="center"
        />

        <ImageView
            android:layout_width="340dp"
            android:layout_height="340dp"
            android:layout_gravity="center"
            android:background="#00ff00"
            />
            <ImageView
                android:layout_width="320dp"
                android:layout_height="320dp"
                android:background="#0000ff"
                android:layout_gravity="center"
                />
        <ImageView
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:background="#FFFF6600"
            android:layout_gravity="center"
        />
        <ImageView
            android:layout_width="280dp"
            android:layout_height="280dp"
            android:background="#FF00FFD9"
            android:layout_gravity="center"
        />

</FrameLayout>


3.表格布局
表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数
结果如下:
实现代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:text="这是第一个lay的第一行"
        />
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#ff0000"
                android:text="2行二1列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:text="2行二2列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:layout_weight="1"
                android:text="2行二3列"
                />
        </TableRow>
</TableLayout>
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1"
        >
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二个lay的第一行第1列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二个lay的第一行第2列"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二个lay的第一行第3列"
                />
        </TableRow>
        <TableRow>
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二行1列"
                />
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二行2列"
                />
            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:background="#00ff00"
                android:text="这是第二行3列"
                />
        </TableRow>
</TableLayout>
</TableLayout>
4.相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。
基本用法都在图片中:

结果如下:

原代码如下:

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:id="@+id/tv_main_blue"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_toRightOf="@id/tv_main_blue"
        android:layout_below="@id/tv_main_blue"
        android:id="@+id/tv_main_bule2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:layout_toRightOf="@id/tv_main_bule2"

        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮4"
        android:id="@+id/tv_main_blue4"
        android:layout_below="@id/tv_main_bule2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮5"
        android:id="@+id/tv_main_blue5"
        android:layout_below="@id/tv_main_bule2"
        android:layout_toRightOf="@id/tv_main_bule2"
        />
</RelativeLayout>

5. 绝对布局
 绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值