相对布局名为RelativeLayout,因为下级视图的位置是相对位置,所以得有具体的参照物才确定最终位置。如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。
相对位置的属性取值 and 相对位置说明
layout_toLeftOf
当前视图在指定视图的左边
layout_toRightOf
当前视图在指定视图的右边
layout_above
当前视图在指定视图的上方
layout_below
当前视图在指定视图的下方
layout_alignLeft
当前视图与指定视图的左侧对齐
layout_alignRight
当前视图与指定视图的右侧对齐
layout_alignTop
当前视图与指定视图的顶部对齐
layout_alignBottom
当前视图与指定视图的底部对齐
layout_centerInParent
当前视图在上级视图中间
layout_centerHorizontal
当前视图在上级视图的水平方向居中
layout_centerVertical
当前视图在上级视图的垂直方向居中
layout_alignParentLeft
当前视图与上级视图的左侧对齐
layout_alignParentRight
当前视图与上级视图的右侧对齐
layout_alignParentTop
当前视图与上级视图的顶部对齐
layout_alignParentBottom
当前视图与上级视图的底部对齐
layout centerVertical
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="150dp" > <TextView android:id="@+id/tv_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#ffffff" android:text="我在中间" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:background="#eeeeee" android:text="我在水平中间" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="#eeeeee" android:text="我在垂直中间" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_parent_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:background="#eeeeee" android:text="我跟上级左边对齐" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_parent_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="#eeeeee" android:text="我跟上级右边对齐" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_parent_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="#eeeeee" android:text="我跟上级顶部对齐" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_parent_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#eeeeee" android:text="我跟上级底部对齐" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_left_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/tv_center" android:layout_alignTop="@+id/tv_center" android:background="#eeeeee" android:text="我在中间左边" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_right_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tv_center" android:layout_alignBottom="@+id/tv_center" android:background="#eeeeee" android:text="我在中间右边" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_above_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/tv_center" android:layout_alignLeft="@+id/tv_center" android:background="#eeeeee" android:text="我在中间上面" android:textSize="11sp" android:textColor="#000000" /> <TextView android:id="@+id/tv_below_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_center" android:layout_alignRight="@+id/tv_center" android:background="#eeeeee" android:text="我在中间下面" android:textSize="11sp" android:textColor="#000000" /> </RelativeLayout>
效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端