Android:Layouts介绍

本文介绍了Android中常用的几种布局管理器,包括FrameLayout、LinearLayout、RelativeLayout等,并详细解释了它们的特点及应用场景。

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

Layout管理器(一般称“layouts”)是ViewGroup的扩展,被设计用来控制子控件在屏幕上的位置。Layouts能相互嵌套,通过Layout管理器的结合,你可以创建任意复杂的界面。

 

Android SDK包含一些简单的Layout,帮助你构建UI。选择layout间正确的结合来使界面易于理解和使用就取决于你咯。

 

下面的列表包含了一些更通用的layout类:

 

 FrameLayout

最简单的layout管理器,FrameLayout简单地将每一个View显示在左上角。增加多个子View时,新的View位于先前的上方,并且还遮盖先前的View

 

 LinearLayout

LinearLayout用来沿着某一个直线方向增加子View,包括垂直的和水平的。垂直的layout每一行有一个孩子;而水平的layout只有一行。LinearLayout可以为每个子View指定“weight”,来控制在可获得的空间里每个子View的相对大小。


 RelativeLayout

使用RelativeLayout,你可以定义每个子View相对于其它的View或屏幕边界的位置。

 

 TableLayout

TableLayout允许你按照网格的行列来放置View。每格可以占据多个行和列,并且列可以设定为缩小或增长。


 AbsoluteLayout

AbsoluteLayout里,每个子View的位置以绝对坐标进行定义。使用这个类,你可以为组件安排最精确的布局,但是有代价的。与之前的管理器比较而言,以绝对的坐标来定义layout意味着你的layout不能为不同的屏幕分辨率和方向进行动态调整。

 

Android文档详细的描述了每个layout的特性和属性,所以不在这里重复说明,我建议你可以参见:

 

http://code.google.com/android/devel/ui/layout.html.

 

在这章的后面,你会学习到如何通过扩展这些layout类来创建组合的控件(widget由一些互相联系的View组成)。

 

one or more layouts are missing the layout_width or layout_height attributes以下代码出现这个问题<?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:layout_gravity="center" android:background="@drawable/animal_bg" android:orientation="vertical" android:paddingLeft="15dp" android:paddingRight="15dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="200dp" android:orientation="horizontal"> <Button style="@style/btnStyle" android:background="@drawable/three"/> <Button style="@style/btnStyle" android:background="@drawable/four" /> <Button style="@style/btnStyle" android:background="@drawable/box" /> <Button style="@style/btnStyle" android:background="@drawable/five"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="15dp" android:orientation="horizontal"> <Button style="@style/btnStyle" android:background="@drawable/one" /> <Button style="@style/btnStyle" android:background="@drawable/two"/> <Button style="@style/btnStyle" android:background="@drawable/box"/> <Button style="@style/btnStyle" android:background="@drawable/four"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="15dp" android:orientation="horizontal"> <Button style="@style/btnStyle" android:background="@drawable/five" /> <Button style="@style/btnStyle" android:background="@drawable
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值