相对布局概述 RelativeLayout
--- 最灵活的布局
--- 描述容器与控件的位置关系
--- 描述控件与控件的位置关系
相对布局的常用属性:
sa
注:先出现的id要添加+号,再次引用则不必带加号.
通常与居中属性联用 android:layout_alignParentLeft为默认
扫描二维码关注公众号,回复:
2543578 查看本文章
以上七条指令使用时使其值为true即可.
示例1:
<?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"> <ImageView android:id="@+id/iv_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@mipmap/ic_15" /> <ImageView android:id="@+id/iv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:src="@mipmap/ic_4" /> <ImageView android:id="@+id/iv_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@mipmap/ic_6" /> <ImageView android:id="@+id/iv_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@mipmap/ic_10" /> <ImageView android:id="@+id/iv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@mipmap/ic_2" /> </RelativeLayout> 效果图:
示例2:
<?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">
<!--头部-->
<RelativeLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#00f">
</RelativeLayout>
<!--底部-->
<RelativeLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#0ff"
android:layout_alignParentBottom="true">
</RelativeLayout>
<!--中间部分-->
<RelativeLayout
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0"
android:layout_below="@id/top"
android:layout_above="@id/bottom" //layout_above属性必须设置,否则将会被底部遮掩
>
</RelativeLayout>
子布局在XML中的顺序不重要(可以先声明底部容器)
该代码有误,需把红框内容放到setContentView(R.layout.main);之前.
示例3:
<?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"
android:background="#ccc">
<RelativeLayout
android:id="@+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/logo"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@mipmap/book"
android:layout_centerVertical="true"/>
<Button
android:id="@+id/btn"
android:layout_width="66dp"
android:layout_height="30dp"
android:text="卸载"
android:textSize="14sp"
android:textColor="#fff"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@mipmap/bg5"
/>
<RelativeLayout
android:id="@+id/text_box"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/logo"
android:layout_toLeftOf="@id/btn"
android:layout_centerVertical="true"
>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记事本"
android:textSize="16sp"
android:textColor="#000"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="版本号: 1.0"
android:textSize="12sp"
android:textColor="#999"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大小: 3.0 M"
android:textSize="12sp"
android:textColor="#999"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Relativelayout&&LinearLayout
<?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" android:background="#ccc"> <!--行布局--> <RelativeLayout android:id="@+id/box" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:background="#fff" android:padding="5dp" > <ImageView android:id="@+id/logo" android:layout_width="70dp" android:layout_height="70dp" android:src="@mipmap/book" android:layout_centerVertical="true" /> <Button android:id="@+id/btn" android:layout_width="66dp" android:layout_height="30dp" android:text="卸载" android:textColor="#fff" android:textSize="14sp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@mipmap/bg4" /> <!--文字说明部分--> <LinearLayout android:id="@+id/text_box" android:layout_width="match_parent" android:layout_height="70dp" android:layout_toRightOf="@id/logo" android:layout_toLeftOf="@id/btn" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记事本" android:layout_marginTop="3dp" android:textSize="16sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="版本: 1.0" android:layout_marginTop="6dp" android:textSize="12sp" android:textColor="#999" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大小: 3.0M" android:layout_marginTop="6dp" android:textColor="#999" android:textSize="12sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>