浅析Android App的相对布局RelativeLayout
作者:剑萧舞蝶 时间:2023-04-08 18:28:48
一、什么是相对布局
相对布局是另外一种控件摆放的方式
相对布局是通过指定当前控件与兄弟控件或者父控件之间的相对位置,从而达到相对的位置
二、为什么要使用相对布局
相对于线性布局ui性能好
三、相对布局的两组常用属性
值为某个存在控件id:
(1)android:layout_below放在某个存在id控件的下边缘(也就是当前控件的上边对齐到某个id控件的下边缘
(2)android:layout_above放在某个存在id控件的上边缘(也就是当前控件的下边缘对齐到某个id控件的上边缘
(3)android:layout_toLeftOf 放在某个存在id控件的左边(也就是当前控件的右边对齐到某个id控件的左边
(4)android:layout_toRightOf 放在某个存在id控件的右边(也就是当前控件的左边对齐到某个id控件的右边)
(5)android:layout_alignLeft 当前的控件左边缘对齐到某个存在的id控件的左边缘
(6)android:layout_alignRigth 当前的控件右边缘对齐到某个存在的id控件的右边缘
(7)android:layout_alignTop 当前的控件上边缘对齐到某个存在的id控件的上边缘
(8)android:alignBottom 当前的控件下边缘对齐到某个存在的id控件的下边缘
1.对齐至控件的基准线
基准线是为了保证印刷字母的整齐而划定的线
值为某个存在控件的id:
android:layout_alignBaseline
2.与父控件的四个边缘对齐
值为true or false:
(1)android:layout_aliginParentLeft 对齐到父控件的左边
(2)android:layout_alignParentRight 对钱对齐到父控件的右边
(3)android:layout_alignParentTop对齐到父控件的上边
(4)android:layout_alignParentBottom 对齐到父控件的下边
3.对齐至父控件的中央位置
值为 true or false:
(1)android:layout_centerInParent 对齐到父控件的最中央位置
(2)android:layout_layout_Horizontal 对齐到父控件水平方向中央的位置
(3)android:layout_centerVertical 对齐到父控件垂直方向中央的位置
4.android 4.2 Relativelayout布局的新属性
值为某个存在控件的di:
(1)android:layout_alignStart 当前控件的起始位置对对齐到某个存在控件id的起始位置
(2)android:layout_alignEnd 当前控件的起始位置对对齐到某个存在控件id的对齐到终点位置
值为true or false:
(1)android:layout_alignParentStart 对齐到父控件的起始位置
(2)android:layout_alignParentEnd 对齐到父控件的终点位置
5.字体居中
android:gravity="center"
默认值:
android:hint="值"
andriod:inputType="textpassworld"
四、相对布局实例—登录界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/Txttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal"
android:layout_alignParentRight="true"
android:text="登录界面"/>
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/Txttitle"
android:layout_alignRight="@id/Txttitle"
android:layout_below="@id/Txttitle"
android:layout_marginTop="20dp"
android:hint="username"/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/username"
android:layout_alignLeft="@id/username"
android:layout_alignRight="@id/username"
android:layout_marginTop="20dp"
android:hint="password"
android:inputType="textCapWords"/>
</RelativeLayout>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
字符串阵列String[]转换为整型阵列Int[]的实例
![](https://img.aspxhome.com/file/2023/1/116531_0s.jpg)
C#把UNICODE编码转换为GB编码的实例
Javafx实现国际象棋游戏
![](https://img.aspxhome.com/file/2023/3/129193_0s.png)
SpringBoot @CompentScan excludeFilters配置无效的解决方案
Java中File类方法详解以及实践
![](https://img.aspxhome.com/file/2023/5/65605_0s.jpg)
android 判断网络是否可用与连接的网络是否能上网
详解Java中clone的写法
![](https://img.aspxhome.com/file/2023/6/99766_0s.jpg)
WinForm 自动完成控件实例代码简析
![](https://img.aspxhome.com/file/2023/7/108617_0s.png)
java中获取json的所有key方法
Android自定义LinearLayout布局显示不完整的解决方法
![](https://img.aspxhome.com/file/2023/4/102594_0s.png)
探讨:如何使用委托,匿名方法对集合进行万能排序
Android Studio 新建项目通过git上传到码云图文教程详解
![](https://img.aspxhome.com/file/2023/5/92595_0s.png)
Java内存区域管理详解
![](https://img.aspxhome.com/file/2023/3/59173_0s.png)
9个非常棒的Android代码编辑器 移动开发者的最爱
![](https://img.aspxhome.com/file/2023/9/137669_0s.jpg)
SpringBoot使用Log4j过程详解
C#操作注册表之Registry类
JDBC实现学生管理系统
C# 如何使用ajax请求
深入理解C# DateTime日期格式化
![](https://img.aspxhome.com/file/2023/1/126091_0s.png)