Android之RelativeLayout

Android布局基本知识

我们知道,在布局文件中定义一个控件(包括布局控件),最基本的就是要定义控件的位置(相对于父控件或者同级控件)和大小。大小包括宽度和高度,位置包括上、下、左、右。位置和大小会相互制约,相互影响。

在布局文件中必须定义控件的初始大小,也就是宽度和高度,而位置则默认为左上角。

Android的View类中几个与位置有关的方法

1.getTop()。上边框相对于父控件上边框的距离。

2.getBottom()。下边框相对于父控件上边框的距离。

3.getLeft()。左边框相对于父控件左边框的距离。

4.getRight()。右边框相对于父控件左边框的距离。

所以:

高度=getBottom() - getTop();

宽度=getRight() - getLeft()。

RelativeLayout的子控件生效属性

// 相对于给定ID控件

android:layout_above 将该控件的底部置于给定ID的控件之上;

android:layout_below 将该控件的底部置于给定ID的控件之下;

android:layout_toLeftOf    将该控件的右边缘与给定ID的控件左边缘对齐;

android:layout_toRightOf  将该控件的左边缘与给定ID的控件右边缘对齐;


android:layout_alignBaseline  将该控件的baseline与给定ID的baseline对齐;

android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;

android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;

android:layout_alignLeft        将该控件的左边缘与给定ID的左边缘对齐;

android:layout_alignRight      将该控件的右边缘与给定ID的右边缘对齐;

// 相对于父组件

android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐;

android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;

android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐;

android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;

// 居中

android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;

android:layout_centerVertical     如果为true,将该控件的置于垂直居中;

android:layout_centerInParent   如果为true,将该控件的置于父控件的中央;

// 指定势力范围

android:layout_marginTop      上部势力范围;

android:layout_marginBottom 下部势力范围;

android:layout_marginLeft   左部势力范围;

android:layout_marginRight   右部势力范围;

注意:如果将android:layout_alignParentTop、android:layout_alignParentBottom、android:layout_alignParentLeft和android:layout_alignParentRight都设置为true,将android:layout_marginTop、android:layout_marginBottom、android:layout_marginLeft和android:layout_marginRight都设置为100dp,那么子控件并不会充满父控件,而是子控件的上下左右都会有100dp的空白(势力范围),这说明margin的优先级高于alignParent。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: RelativeLayoutAndroid Studio 中的一种布局类型,它允许您根据相对位置来定位控件。例如,您可以将一个按钮设置为相对于屏幕顶部的特定距离,或者将一个文本框相对于另一个文本框的右侧。这种布局类型可以帮助您更灵活地控制应用程序的外观和布局。 ### 回答2: RelativeLayoutAndroid Studio中自带布局之一,是比较常用的一种布局方式。相对布局就是指控件相对于其他控件的位置而布局,即控件的位置和大小是由其与其他控件之间的相对关系来确定的。这种布局方式比线性布局更加灵活,可以满足大部分UI界面布局需求。 使用RelativeLayout布局需要先在XML布局文件中定义控件的位置和大小。相对参照对象包括父容器和其他子控件,可以使用各种相对位置属性来确定控件的位置,如alignParentTop、alignParentBottom、alignParentLeft、alignParentRight。同时,子控件之间可以使用layout_below、layout_above、layout_toLeftOf、layout_toRightOf等属性来设定控件之间的关系。 在使用RelativeLayout布局时,需要根据具体界面设计需求,结合UI设计稿选择合适的参照对象和相对位置属性。同时,需要注意控件之间的相对关系,避免出现布局错乱等错误。 相对布局的好处在于对于各种大小的屏幕,它可以灵活自如地移动内容。因为,相对布局是以相对关系而非绝对位置进行布局的,所以可以移动到适当的位置而适应不同的屏幕大小和分辨率。相对布局还可以帮助开发人员处理管理内容之间的复杂关系,这让它成为开发复杂应用程序的理想方式。 综上所述,RelativeLayout布局是Android Studio中常用的一种布局方式,可以满足大部分UI界面布局需求。相对布局具有灵活性和适应性,可以根据不同的屏幕大小和分辨率来移动内容,有助于开发人员处理管理内容之间的复杂关系,为开发复杂应用程序提供了比较理想的方式。 ### 回答3: RelativeLayoutAndroid Studio中常用的一种布局方式,用于相对位置的排列控件。如其名,RelativeLayout基于相对位置而非绝对位置的坐标系统来排列控件,能够灵活适应不同屏幕尺寸的设备。 在RelativeLayout中,每一个控件都可以与其它控件或布局的边缘设置相对位置,如上方、下方、左方、右方等,也可以同时与多个控件设置相对位置。如下面的代码所示,使用android:layout_below属性将TextView控件放置在另一个TextView控件之下: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top text view" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom text view" android:layout_below="@id/textView1" /> </RelativeLayout> ``` 除了上下左右之外,RelativeLayout还支持控件左上、左下、右上、右下四个角之间的相对位置。例如,使用android:layout_alignParentRight属性将ImageView控件放置在RelativeLayout的父布局的右侧: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/my_image" android:layout_alignParentRight="true" /> </RelativeLayout> ``` RelativeLayout也可以使用android:layout_alignBaseline属性来将多个文本控件的baseline对齐,从而使它们的文本在水平方向上具有相同的字体大小和位置。 总之,RelativeLayoutAndroid Studio中是一个十分常用的布局方式,它可以根据不同的需求为开发者提供更加灵活和多样化的界面布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值