Android RelativeLayout布局

来源:互联网 发布:复制目录 linux 编辑:程序博客网 时间:2024/06/05 19:57

Android RelativeLayout布局

相对布局(RelativeLayout)将子视图元素以相对位置显示。

1、居中显示

layout_centerInParent:相对于父元素完全居中
layout_centerHorizontal:相对于父元素水平居中
layout_centerVertical:相对于父元素垂直居中

<?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="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="centerInParent"        android:background="#ffa6a5aa"/>    <!-- 水平居中 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:text="centerHorizontal"        android:background="#ffa6a5aa"/>    <!-- 垂直居中 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:text="centerVertical"        android:background="#ffa6a5aa"/></RelativeLayout>

显示如下


2、相对于父控件的位置

layout_alignParentTop:父元素的上边
layout_alignParentBottom:父元素的下边
layout_alignParentLeft:父元素的左边
layout_alignParentRight:父元素的右边

<?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="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="Left|Top"        android:background="#ffa6a5aa"/>    <!-- 右上角显示 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:text="Right|Top"        android:background="#ffa6a5aa"/>    <!-- 左下角显示 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentBottom="true"        android:text="Left|Bottom"        android:background="#ffa6a5aa"/>    <!-- 右下角显示 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentBottom="true"        android:text="Right|Bottom"        android:background="#ffa6a5aa"/></RelativeLayout>

显示如下


3、相对于其他控件的位置

layout_above:某元素的上边
layout_below:某元素的下边
layout_toRightOf:某元素的右边
layout_toLeftOf:某元素的左边

<?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:id="@+id/tv_center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="center"        android:textSize="32dp"        android:background="#ffffcc00"/>    <!-- 显示在左上角 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toLeftOf="@id/tv_center"        android:layout_above="@id/tv_center"        android:text="above|toLeftOf"        android:background="#ffa6a5aa"/>    <!-- 显示在右下角 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/tv_center"        android:layout_below="@id/tv_center"        android:text="below|toRightOf"        android:background="#ffa6a5aa"/></RelativeLayout>

显示如下


4、对齐方式

layout_alignTop:上边与某元素的上边对齐
layout_alignBottom:下边与某元素的下边对齐
layout_alignLeft:左边与某元素的左边对齐
layout_alignRight:右边与某元素的右边对齐
layout_alignBaseline:基准线与某元素的基准线对齐

<?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:id="@+id/tv_center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="center"        android:textSize="32dp"        android:background="#ffffcc00"/>    <!-- 左对齐 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@id/tv_center"        android:layout_above="@id/tv_center"        android:text="alignLeft"        android:background="#ffa6a5aa"/>    <!-- 右对齐 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@id/tv_center"        android:layout_below="@id/tv_center"        android:text="alignRight"        android:background="#ffa6a5aa"/>    <!-- 上对齐 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@id/tv_center"        android:layout_toLeftOf="@id/tv_center"        android:text="alignTop"        android:background="#ffa6a5aa"/>    <!-- 下对齐 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@id/tv_center"        android:layout_toRightOf="@id/tv_center"        android:text="alignBottom"        android:background="#ffa6a5aa"/>    <!-- 基准线对齐 -->    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@id/tv_center"        android:text="alignBaseline"        android:layout_toLeftOf="@id/tv_center"        android:background="#ffa6a5aa"/></RelativeLayout>

显示如下



0 0
原创粉丝点击