yetang307

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  129 随笔 :: 0 文章 :: 1 评论 :: 2391 阅读
相对布局名为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>
复制代码

效果:

 

 

 

posted on   椰糖  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示