android 自定义relativelayout,Android布局(RelativeLayout、TableLayout等)使用方法

本文详细介绍了Android界面开发中的四大基础布局:LinearLayout(线性布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)和TableLayout(表格布局),包括它们的使用方法、常用属性及其示例。通过示例展示了如何控制控件的位置和排列方式,以及在不同场景下的应用。

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

本文介绍 Android 界面开发中最基本的四种布局LinearLayout、RelativeLayout、FrameLayout、TableLayout 的使用方法及这四种布局中常用的属性。

LinearLayout 线性布局,布局中空间呈线性排列

RelativeLayout 相对布局,通过相对定位的方式,控制控件位置

FrameLayout 帧布局,最简单的布局,所有控件放置左上角

TableLayout 表格布局,以行列方式控制控件位置

四种布局示例

1.LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="150dp"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="垂直1" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="垂直2" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="水平1" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="水平2" />

android:layout_width="match_parent"

android:layout_height="150dp"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="top"

android:text="水平上对齐" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:text="水平垂直居中" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:text="水平下对齐" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="3"

android:hint="请输入..."/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:text="提交" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:hint="请输入..."/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="提交" />

orientation:horizontal(水平)/vertical(垂直),表示线性排列的方向。

layout_width/layout_height:元素的宽度与高度

layout_gravity:top/bottom/center/left/right/etc,表示当前元素相对父元素的对齐方式,多种对齐方式用“|”隔开,右上对齐:top|right。

layout_weight:占据空间的比例,例如元素A和B,A设置为1,B设置为3, 元素A、B分别占空间的1/4、3/4,此时元素宽度不由layout_width决定,设置为0dp是比较规范的写法。

layout_weight 若元素A设置为1,元素B不设置,将layout_width设置为具体的值或wrap_content,那么元素B的宽度由layout_width决定,元素A将占满屏幕剩下的空间。

2.RelativeLayout

android:layout_width="match_parent"

android:layout_height="300dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentBottom="true"

android:text="我在左下"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="我在中间"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:text="我在右上"/>

android:layout_width="match_parent"

android:layout_height="300dp">

android:id="@+id/button_2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text="参照按钮"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@id/button_2"

android:layout_toRightOf="@id/button_2"

android:text="我在右上"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/button_2"

android:layout_toLeftOf="@id/button_2"

android:text="我在左下"/>

以下属性值为true/false

layout_centerHorizontal/layout_centerVertical: 水平居中、垂直居中

layout_centerInparent: 相对父元素垂直&水平居中

layout_alignParentBottom: 元素下边界和父元素下边界对齐

layout_alignParentLeft: 左边界对齐

layout_alignParentRight: 右边界对齐

layout_alignParentTop: 上边界对齐

以下属性值为控件id

layout_above/layout_below: 在某元素的上方/下方

layout_toLeftOf/layout_toRightOf: 在某元素的左方/右方

layout_alignTop/layout_alignBottom: 元素上(下)边界与某元素上(下)边界对齐

layout_alignLeft/layout_alignRight: 左(右)边界对齐

3.FrameLayout

所有元素都放置在布局的左上角

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="我是一个按钮"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="我是一个输入框"/>

4.TableLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_height="wrap_content"

android:text="邮箱"/>

android:layout_height="wrap_content"

android:inputType="textEmailAddress"

android:hint="请输入您的邮箱" />

android:layout_height="wrap_content"

android:text="密码"/>

android:layout_height="wrap_content"

android:inputType="textPassword"

android:hint="请输入密码" />

android:layout_height="wrap_content"

android:layout_span="2"

android:text="注册" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:stretchColumns="1">

...

TableRow:代表表格布局的一行,行内一个元素代表一列。

layout_span:合并单元格,设置为2,代表该元素占据2列空间。

stretchColumns: TableRow中无法指定空间宽度,那么需要用到该属性,设置为1,表示拉伸第2列(0为第1列)与屏幕一样宽,效果如TableLayout的第二张图。

5.自定义布局

Android中,布局下可以放置控件,也可以放置子布局。如果子布局内容较为独立且经常使用,例如标题栏,或者布局比较复杂,这时候可以考虑使用自定义布局的形式导入。方法很简单。

新建一个布局文件,例如example.xml

在父布局中引入:

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

以上就是Android最基本的四种布局的详细内容介绍,希望对大家的学习有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值