Android布局之RelativeLayout学习

本文详细介绍了Android中相对布局的关键属性,并通过代码示例展示了如何利用这些属性来布局控件,包括如何设置控件的位置关系如上、下、左、右、对齐方式及基准线对齐等。

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

先来看一下相对布局的主要属性有哪些:

Android:layout_above 将该控件的底部置于给定ID的控件之上;

Android:layout_below 将该控件的底部置于给定ID的控件之下;

Android:layout_toLeftOf    将该控件的右边缘与给定ID的控件左边缘对齐;

Android:layout_toRightOf  将该控件的左边缘与给定ID的控件右边缘对齐;

-------------------------------------------------------------------------

Android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐;

Android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;

Android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐;

Android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;

----------------------------------------------------------------------------------

Android:layout_alignBaseline  将该控件的baseline与给定ID的baseline对齐;

Android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;

Android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;

Android:layout_alignLeft        将该控件的左边缘与给定ID的左边缘对齐;

Android:layout_alignRight      将该控件的右边缘与给定ID的右边缘对齐;

-------------------------------------------------------------------------------

代码示例:

先看效果:

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"
     >
    <TextView 
        android:id="@+id/username"
        android:textSize="16dp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="@string/username"
        />
    <EditText 
        android:id="@+id/input_user"
        android:hint="@string/input_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/username"
        />
    <TextView 
        android:id="@+id/password"
        android:textSize="16dp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/input_user"
        android:text="@string/password"
        />

    <EditText
        android:id="@+id/input_pwd"
        android:hint="@string/input_pwd"
        android:inputType="textPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
         />

    <Button 
        android:text="@string/ok"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:id="@+id/ok"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/input_pwd"
        />

    <Button
        android:id="@+id/cancl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/input_pwd"
        android:layout_toLeftOf="@+id/ok"
        android:text="@string/cancl"
        android:textSize="16dp" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button 
        android:id="@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16dp"
        android:text="@string/button"
        />
    <Button 
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_toRightOf="@id/button"
        android:layout_below="@id/button"
        android:text="@string/button1"
        />
    <Button 
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_below="@id/button"
        android:layout_toLeftOf="@id/button"
        android:text="@string/button2"
        />
    <Button 
        android:id="@+id/button3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_toRightOf="@id/button"
        android:layout_above="@id/button"
        android:text="@string/button3"
        />
    <Button 
        android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="16dp"
        android:layout_toLeftOf="@id/button"
        android:layout_above="@id/button"
        android:text="@string/button4"
        />
    
</RelativeLayout>

工具:eclipse3.7+adt 20测试成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值