用相对布局五个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>
本文详细介绍了如何使用相对布局在Android中排列五个Button按钮形成梅花形状,包括每个按钮的具体属性配置和布局关系。

被折叠的 条评论
为什么被折叠?



