android布局之相对布局(RelativeLayout)

用相对布局五个Button按钮实现一个梅花的形状,如图,

直奔主题,代码实现,在布局文件中:
<?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:id="@+id/top1"为button添加id,

		android:layout_marginTop="30dp"距离容器顶端30dp
		android:layout_alignParentRight="true"位置靠右
		android:layout_marginRight="60dp"距离容器右边框60dp

	 -->

    <Button 
        android:id="@+id/top1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="梅花"
        android:layout_marginTop="30dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="60dp"/>

	<!--第二个组件
		android:id="@+id/top2"为button添加id,
		android:layout_toLeftOf="@id/top1"组件的位置在id=top1组件的左边,即相对与第一个Button的左边

		android:layout_alignBaseline="@id/top1" 和id为top1的组件在同一基准线上,也可以理解成在同一条直线上
		android:layout_marginRight="100dp"组件位置距离容器右边框100dp远	



	 -->
<Button android:id="@+id/top2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="梅花" android:layout_toLeftOf="@id/top1" android:layout_alignBaseline="@id/top1" android:layout_marginRight="100dp"/>

	<!--第三个组件
		android:id="@+id/center"为组件添加id,

       	 	android:layout_below="@+id/top2"相对与id为top2的组件(即第二个按钮)在其下面,
       		android:layout_centerHorizontal="true"  设置组件相对于容器水平居中
        	android:layout_marginTop="40dp"	组件距离容器右边框40dp

	 -->
<Button android:id="@+id/center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/top2" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="梅花" />
	<!--第四个组件
		android:id="@+id/down1" 为组件添加ID
        	android:layout_below="@id/center"相对于id为center的组件(即第三个按钮)在其下面
		android:layout_alignParentRight="true"位置靠右
		android:layout_marginRight="60dp"距离容器右边框60dp




	 -->
<Button android:id="@+id/down1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="梅花" android:layout_below="@id/center" android:layout_alignParentRight="true" android:layout_marginRight="60dp"/>
	<!--第五个组件
		android:id="@+id/down2"为button添加id,
		android:layout_toLeftOf="@id/down1"组件的位置在id=down1组件的左边,即相对与第一个Button的左边

		android:layout_alignTope="@id/down1" 和id为down1的组件的顶端对齐
		android:layout_marginRight="100dp"组件位置距离容器右边框100dp




	 -->
<Button android:id="@+id/down2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="梅花" android:layout_toLeftOf="@id/down1" android:layout_alignTop="@id/down1" android:layout_marginRight="100dp"/></RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值