android 相对布局

来源:互联网 发布:白芨吉利知胃病中草药 编辑:程序博客网 时间:2024/06/04 19:31

这个布局类型常用的属性有点多
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   右偏移的值;

看看效果吧,因为属性太多所以就没有一个个的区使用了,只用了个别做代表

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:text="aaa"        android:background="#ff0000"        android:textSize="20sp"        android:gravity="center"        />     <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:text="bbb"        android:background="#faffae00"        android:textSize="20sp"        android:gravity="center"         android:layout_centerHorizontal="true"        />    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:text="ccc"        android:background="#faffee00"        android:textSize="20sp"        android:gravity="center"         android:layout_alignParentRight="true"        />    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:text="ddd"        android:background="#fa0015ff"        android:textSize="20sp"        android:gravity="center"        android:layout_centerInParent="true"        android:layout_margin="10dp"        android:id="@+id/TV_main_blue"        />    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:text="eee"        android:background="#fa2c2c2a"        android:textSize="20sp"        android:gravity="center"        android:layout_toLeftOf="@id/TV_main_blue"        android:layout_centerVertical="true"        />     <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:text="eee"        android:background="#faff00b2"        android:textSize="20sp"        android:gravity="center"        android:layout_toRightOf="@id/TV_main_blue"        android:layout_alignBottom="@id/TV_main_blue"        />    <TextView        android:layout_width="match_parent"        android:layout_height="50dp"        android:text="eee"        android:background="#fa05f6ea"        android:gravity="center"        android:layout_alignParentBottom="true"        /></RelativeLayout>

效果:

这里写图片描述