[Android]RelativeLayout布局常用属性

本文通过一个具体的RelativeLayout布局文件实例,详细介绍了各个属性的作用,如水平居中、垂直居中、组件间的位置关系等。

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

先看一下一个布局文件例子:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <TextView 
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="RelativeLayout 布局"
            android:gravity="center_horizontal"
            android:layout_centerHorizontal="true"
            android:id="@+id/title"
            />
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_below="@id/title"
            android:id="@+id/line"
            android:background="#ffffff"
            />
        <Button 
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/line"
            android:id="@+id/btn_1"
            android:layout_marginLeft="10dp"
            android:text="Button One"
            />
        <Button 
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/line"
            android:id="@+id/btn_2"
            android:layout_toRightOf="@id/btn_1"
            android:text="Button Two"
            />

    </RelativeLayout>

android:gravity="center_horizontal"   文字水平居中
android:layout_centerHorizontal="true" 组件水平居中,在相对布局中layout_gravity是不好使的,类似属性还有:android:layout_centerVertical="true"
android:layout_below="@id/title"  分割线位于标题(id=title)下边,类似的属性还有:android:layout_above
android:layout_toRightOf="@id/btn_1" 按钮2位于按钮1右边,类似的属性还有:android:layout_toLeftOf
android:layout_marginLeft="10dp" 组件距离父组件左边10dp,类似的属性还有android:layout_marginRight、android:layout_marginTop、android:layout_marginBottom、android:layout_margin

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值