04、Android开发基础之相对布局

来源:互联网 发布:淘宝虚拟物品仅退款 编辑:程序博客网 时间:2024/06/07 01:25

Android开发基础之相对布局

相对布局,也就是要有参照物的布局。小时候 我们上学,同学与同学之间,谁在谁的右边,谁在谁的左边!这就是相对于谁来说的!在Android中的相对布局也一样!

有两种,一种是相对于老爸来说,也就是父控件来说的,另外一种是相对于同级控件来说的!

相对布局相对于父控件

<?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">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="中间"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:text="右上角"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="右下角"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="左上角"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:text="左下角"/></RelativeLayout>

图片 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">    <!--这个是中间部分-->    <Button        android:id="@+id/center_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="确定"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toLeftOf="@id/center_button"        android:text="我在中间的左边"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@id/center_button"        android:layout_centerHorizontal="true"        android:text="我在中间的顶部"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toRightOf="@id/center_button"        android:text="我在中间的右边"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/center_button"        android:layout_centerHorizontal="true"        android:text="我在中间的底部"/></RelativeLayout>

图片 1

视频下载地址

视频下载地址:

链接: https://pan.baidu.com/s/1o8G7hCe 密码: 7qnt

欢迎关注微信公众号

原创粉丝点击