Android 五大布局之线性布局和相对布局

本文详细介绍了Android的线性布局(LinearLayout)和相对布局(RelativeLayout)的使用,包括各自的属性和用法。线性布局分为横向和纵向布局,通过android:orientation属性调整排列方向。相对布局则利用各种对齐属性实现元素的相对位置摆放,如android:layout_alignParentBottom等。此外,还展示了如何在XML中设置这些属性的例子。

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

Android 五大布局之 线性布局和相对布局 RelativeLayout 的相关的属性 相对布局

第一类 :属性值为true 和false 的:

android:layout_centerHrizontal 水平居中

android:layout_centerVertial 垂直居中

android:layout_centerInparent 相对于父元素完全居中

android:layout_alignParentBottom 贴紧父元素的下边缘

android:layout_alignParentLeft 贴紧父元素的左边缘

android:layout_alignParentRight 贴紧父元素的右边缘

android:layout_alignParentTop 贴父元素的上边缘

android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素作为参照物

第二类:属性值必须为id的引用名,"@id/id-name" 就是参照的元素id

android:layout_below 在某元素的下方

android:layout_above 在某元素的上方

android:layout_toLeftOf 在某元素的左边

android:layout_toRigthOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的上边缘对齐

android:layout_alignLeft 本元素的左边缘和某元素的左边缘对齐

android:layout_alignBottom 本元素的下边缘和某元素的下边缘对齐

android:layout_alignRigth 本元素的右边缘和某元素的右边缘对齐

第三类的:属性的值为具体的像素的值: 如:20dip , 50px

android:layout_marginBottom 离某元素底边缘的距离

android:layout_marginLeft 离某元素左边缘的距离

android:layout_marginRigth 离某元素右边缘的距离

android:layout_marginTop 离某元素上边缘的距离

五大布局之一:线性布局 LinearLayout

横向布局:一行多列 纵向布局:一列多行

本省的属性:

android:background 设置整个布局画面的背景

android:orientation="horizontal" 子元素的排列队形,是横向排列还是纵向排列

android:gravity="bottom" 子元素在布局中的缺省(默认)看起方式

android:padding 设置子元素的彼次连接,中间不留空白

子元素的属性: android:layout_gravity 设置自身对象在父布局中的看齐方式,可以更新父布局对象给定的缺省的值

android:layout_weight 将父布局中剩余的尺寸按各兄弟元素的weight值比例进行填充;

示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center_horizontal"

android:orientation="vertical" >

<LinearLayout

android:layout_marginTop="5dp"

android:layout_gravity="center_horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<TextView

android:id="@+id/tuser"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账户:" />

<EditText

android:id="@+id/euser"

android:layout_width="220dp"

android:layout_height="wrap_content"

/>

</LinearLayout>

<!-- 二行 -->

<LinearLayout

android:layout_gravity="center_horizontal"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码:" />

<EditText

android:id="@+id/epass"

android:layout_width="220dp"

android:layout_height="wrap_content"

/>

</LinearLayout>

<!-- 第三行 -->

<LinearLayout

android:layout_width="fill_parent"

android:layout_marginLeft="20dp"

android:layout_marginTop="20dp"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:orientation="horizontal" >

<Button

android:id="@+id/btnOk"

android:layout_width="86dp"

android:layout_height="35dp"

android:text="登陆" />

<Button

android:layout_marginLeft="30px"

android:id="@+id/btnCancel"

android:layout_width="86dp"

android:layout_height="35dp"

android:text="取消" />

</LinearLayout>

结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值