世间无绝对,Relative即相对-----箴言-----
内容简介:
RelativeLayout相对布局的使用。
上一篇,我们用LinearLayout实现了水平平均布局,今天,来看如何使用RelativeLayout来实现水平平均布局。
(1)水平平均分布,布局在顶端:使用RelativeLayout来实现。
效果图如下:
xml布局文件内容如下:
<?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:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左"
android:layout_alignParentLeft="true"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中"
android:layout_centerHorizontal="true"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右"
android:layout_alignParentRight="true"
>
</Button>
</RelativeLayout>
说明:
android:layout_alignParentLeft="true" :居左
android:layout_centerHorizontal="true":居中
android:layout_alignParentRight="true":居右
(2)水平平均分布,布局在中间:使用RelativeLayout来实现:
效果图:
只需要将gravity设置为center_vertical即可,如下:
<?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:orientation="horizontal"
android:gravity="center_vertical"
>
...
</RelativeLayout>