LinearLayout和RelativeLayout 属性对比

本文深入探讨了Android开发中LinearLayout和RelativeLayout的基本属性,包括控件宽度、高度、排布方式、间距设置、文本样式、背景定义等,并详细解析了两者之间的区别与应用场景。

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

LinearLayout和RelativeLayout 属性对比

共有属性:java代码中通过btn1关联次控件
android:id=”@+id/btn1″


控件宽度
android:layout_width=”80px” //”80dip”或”80dp” 
android:layout_width =“wrap_content”
android:layout_width =“match_parent” 

控件高度
android:layout_height=”80px” //”80dip”或”80dp”
android:layout_height =“wrap_content”
android:layout_height =“match_parent”

控件排布
android:orientation=”horizontal”
android:orientation=”vertical“

控件间距
android:layout_marginLeft=”5dip” //距离左边 
android:layout_marginRight=”5dip” //距离右边
android:layout_marginTop=”5dip” //距离上面
android:layout_marginRight=”5dip” //距离下面
android:paddingLeft=”5dip” 控件显示位置
android:gravity=”center” //left,right, top, bottom 
android:gravity=”center_horizontal”
android:layout_gravity是本元素对父元素的重力方向。
android:layout_gravity属性则设置控件本身相对于父控件的显示位置
android:gravity是本元素所有子元素的重力方向。
android:layout_gravity=”center_vertical”
android:layout_gravity=”left”
android:layout_gravity=”left|bottom”


TextView中文本字体
android:text=”@String/text1″ //在string.xml中定义text1的值 
android:textSize=”20sp” 

android:textColor=”#ff123456”
android:textStyle=”bold” //普通(normal), 斜体(italic),粗斜体(bold_italic)
TextView中,控制其以…结束
android:ellipsize=”end”

只有一行
android:singleLine=”true”

定义控件是否可见
android:visibility=”visible” //可见 
android:visibility=”invisible”  //不可见,但是在布局中占用的位置还在
android:visibility=”gone”   //不可见,完全从布局中消失

定义背景图片
android:background=”@drawable/img_bg” //img_bg为drawable下的一张图片
seekbar控件背景图片及最大值
android:progressDrawable=”@drawable/seekbar_img” 
android:thumb=”@drawable/thumb” 
android:max = “60″
android:layout_alignWithParentIfMissing=”true”


仅在RelativeLayout中有效
在父亲布局的相对位置
android:layout_alignParentLeft=”true” //在布局左边 
android:layout_alignParentRight=”true” //在布局右边 
android:layout_alignParentTop=”true” //在布局上面 
android:layout_alignParentBottom=”true “ //在布局的下面


在某个控件的相对位置
android:layout_toRightOf=”@id/button1″ //在控件button1的右边,不仅仅是紧靠着 
android:layout_toLeftOf=”@id/button1″ //在控件button2的左边,不仅仅是紧靠着 
android:layout_below=”@id/button1 “ //在控件button1下面,不仅仅是正下方 
android:layout_above=“@id/button1” //在控件button1下面,不仅仅是正下方


定义和某控件对奇
android:layout_alignTop=”@id/button1” //和控件button1上对齐 
android:layout_alignBottom=”@id/button1” //和控件button1下对齐 
android:layout_alignLeft=”@id/button1” //和控件button1左对齐 
android:layout_alignRight=”@id/button1” //和控件button2右对齐
android:layout_centerHorizontal=”true” //水平居中 
android:layout_centerVertical=”true”
android:layout_centerInParent=”true” 


仅在LinearLayout中有效
设置控件在一排或一列中所占比例值
android:layout_weight=”1″

### Android RelativeLayout LinearLayout 的用法及区别 #### 属性定义位置的选择 某些属性更适合放置于布局文件中而非样式文件。例如 `android:id` 显然应该位于布局文件内;对于线性布局 (`LinearLayout`) 来说,其方向(`android:orientation`)通常也更适宜在布局文件里指定[^1]。 #### 命名空间声明的重要性 为了能够正常使用由官方提供的组件并享受智能提示功能,在XML布局文件顶部需加入命名空间声明`xmlns:android="http://schemas.android.com/apk/res/android"`。如果省略此行,则虽然编译期不会出现问题,但在实际执行过程中可能会遇到异常情况[^2]。 #### 使用场景对比 ##### LinearLayout 特点 - 子视图按照垂直或水平排列。 - 可通过权重(weight)来分配剩余空间给子元素。 - 更加简单直观,适合构建较为规整的界面结构。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- Child views go here --> </LinearLayout> ``` ##### RelativeLayout 特点 - 提供相对定位方式,允许基于其他控件的位置来进行摆放。 - 能够创建更加灵活复杂的UI设计而不需要过多嵌套层次。 - 对于需要精确控制各个部件间关系的情况尤为适用。 ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- Child views with relative positioning attributes like below, above, etc.--> </RelativeLayout> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值