RelativeLayout相对布局实验

本文介绍了一个使用Android相对布局的实际案例,展示了如何通过不同属性设置按钮之间的相对位置,包括上方、下方、左侧和右侧,并通过示例代码详细解释了实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创 灵思致远 2018-05-16

作者 Leansmall

1. 实验内容简介

相对布局可以设置某一个视图相对于其他视图的位置,这些位置包括上、下、左、右。设置这些位置的属性是android:layout_above、android:layout_below、android:layout_toLeftOf、android:layout_toRightOf。除此之外,还可以通过android:layout_alignBaseline属性设置视图的底端对齐。这5个属性的值必须是存在的资源ID,也就是另一个视图的android:id属性值。

2. UI界面布局

对应的大纲如下:


3. XML代码编写和调试

<?xml version="1.0"encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   android:gravity="center" >

 

    <Button

       android:id="@+id/button1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Button1"

       android:textSize="16dp" />

 

    <Button

       android:id="@+id/button2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@id/button1"

       android:layout_toRightOf="@id/button1"

       android:text="Button2"

       android:textSize="16dp" />

 

    <Button

       android:id="@+id/button3"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@id/button2"

       android:layout_toLeftOf="@id/button2"

       android:text="Button3"

       android:textSize="16dp" />

 

    <Button

       android:id="@+id/button4"

       android:layout_width="wrap_content"

        android:layout_height="wrap_content"

       android:layout_above="@id/button2"

       android:layout_toRightOf="@id/button2"

       android:text="Button4"

       android:textSize="16dp" />

 

    <Button

       android:id="@+id/button5"

        android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_below="@id/button2"

       android:layout_toRightOf="@id/button2"

       android:text="Button5"

       android:textSize="16dp" />

 

</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值