RelativeLayout下级视图的位置可以用与该视图自身平级的视图或者该视图的上级视图的相对位置来描述。如果没有指定参照视图,那么下级视图默认显示在RelativeLayout内部的左上角。相对位置的属性和类型值参照下表:
为了更好的理解上面相对属性的含义,我们使用RelativeLayout来进行一个简单的演示。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.easygoings.androidrelativelayout.MainActivity">
<Button
android:id="@+id/bt_center"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:text="我在父控件中间"/>
<Button
android:id="@+id/bt_center_hor"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:text="我在水平中间"/>
<Button
android:id="@+id/bt_center_ver"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:text="我在垂直中间"/>
<Button
android:id="@+id/bt_par_left"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginTop="100dp"
android:layout_alignParentLeft="true"
android:text="上级左边对齐"/>
<Button
android:id="@+id/bt_par_right"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginTop="100dp"
android:layout_alignParentRight="true"
android:text="上级右边对齐"/>
<Button
android:id="@+id/bt_par_top"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:text="上级顶部对齐"/>
<Button
android:id="@+id/bt_par_bottom"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="上级底部对齐"/>
<Button
android:id="@+id/bt_bottom_left"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_toLeftOf="@+id/bt_par_bottom"
android:layout_alignTop="@+id/bt_par_bottom"
android:text="底部左边"/>
<Button
android:id="@+id/bt_bottom_right"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_toRightOf="@+id/bt_par_bottom"
android:layout_alignTop="@+id/bt_par_bottom"
android:text="底部右边"/>
<Button
android:id="@+id/bt_center_above"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_above="@+id/bt_center"
android:layout_alignLeft="@+id/bt_center"
android:text="中间上边"/>
<Button
android:id="@+id/bt_center_bellow"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_below="@+id/bt_center"
android:layout_alignLeft="@+id/bt_center"
android:text="中间下边"/>
</RelativeLayout>
当然了,也可以再代码中动态添加视图,那么相对位置也只能再代码中定义了。在代码中定义相对位置使用的是 RelativeLayout.LayoutParams的addRule方法
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.bt_add_left:
addNewView(RelativeLayout.ALIGN_PARENT_LEFT,0,content.getId());
break;
case R.id.bt_add_center:
addNewView(RelativeLayout.CENTER_IN_PARENT,0,content.getId());
break;
case R.id.bt_add_right:
addNewView(RelativeLayout.ALIGN_PARENT_RIGHT,0,content.getId());
break;
}
}
private void addNewView(int firstAlign,int secondAlign,int refid)
{
View v = new View(this);
v.setBackgroundColor(0xaa66dd66);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,80);
params.topMargin = 200;
params.addRule(firstAlign,refid);
if(secondAlign > 0 )
{
params.addRule(secondAlign,refid);
}
v.setLayoutParams(params);
v.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View vv) {
content.removeView(vv);
return true;
}
});
content.addView(v);
}