相对布局

来源:互联网 发布:网络黑白 花无涯 下载 编辑:程序博客网 时间:2024/04/27 14:30

相对布局由RelativeLayout所代表,相对布局容器内子组件的位置总是相对兄弟组件、父容器来决定的,因此这种布局方式被称为相对布局。


如果A组件的位置是由B组件的位置来决定的,Android要求先定义B组件,再定义A组件。


实例:梅花布局效果

下面是梅花布局效果的界面布局文件:

<?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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.example.l2112.xiangduibuju.MainActivity"    tools:showIn="@layout/activity_main">    <!-- 定义该组件位于父容器中间 -->    <TextView        android:id="@+id/view01"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="20pt"        android:height="20pt"        android:background="#00f"        android:layout_centerInParent="true" />    <!-- 定义该组件位于view01组件上方 -->    <TextView        android:id="@+id/view02"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="20pt"        android:height="20pt"        android:background="#00f"        android:layout_above="@+id/view01"        android:layout_alignLeft="@+id/view01"/>    <!-- 定义该组件位于view01组件下方 -->    <TextView        android:id="@+id/view03"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="20pt"        android:height="20pt"        android:background="#00f"        android:layout_below="@+id/view01"        android:layout_alignLeft="@+id/view01"/>    <!-- 定义该组件位于view01组件左方 -->    <TextView        android:id="@+id/view04"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="20pt"        android:height="20pt"        android:background="#00f"        android:layout_toLeftOf="@+id/view01"        android:layout_alignTop="@+id/view01"/>    <!-- 定义该组件位于view01组件右方 -->    <TextView        android:id="@+id/view05"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="20pt"        android:height="20pt"        android:background="#00f"        android:layout_toRightOf="@+id/view01"        android:layout_alignTop="@+id/view01"/></RelativeLayout>


0 0
原创粉丝点击