RelativeLayout相对布局

RelativeLayout相对布局

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--相对布局-->
    <!--完成上课代码-->
    <!--TestSize:字体的大小
       Testcolor:字体的颜色
       graving:位置

    -->
    <!--用相对布局来写上下左右-->

    <RelativeLayout
        android:layout_width="250dp"
        android:layout_height="250dp">
        <!--左上:layout_height:左上-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="左上"
            />
        <!--设置二个按钮中间间隔-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:width="7dp"
            />

        <!--右上:layout_alignParenRight:右上-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="右上"
            android:layout_alignParentRight="true"
            />
        <!--中间:layout_centerInParent-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中间"
            android:layout_centerInParent="true"
            />
        <!--左下:layout_alignParentBotton

        -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="左下"
            android:layout_alignParentBottom="true"
            />

        <!--
         左下:layout_alignParenBotton
         右下:layout_alignParenRight
         思路:先设置一个按钮,用相对布局的layout_alignParenBotton来设置,
         用按钮往左下,然后在用layout_alignParenRight来设置,按钮往左边,
        -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="右下"
            />
    </RelativeLayout>

</RelativeLayout>
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RelativeLayout布局是一种相对布局,可以根据父容器来定位子视图的位置。相对布局中的子视图可以根据其他子视图或父容器来确定它们的位置。相对布局的基本格式如下: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> ... </RelativeLayout> ``` 在RelativeLayout中,可以使用一些常见的属性来定位子视图,例如将红色布局放到右上角。相对布局在Android UI开发中应用广泛,尤其在需要复杂布局的场景中,它可以消除嵌套视图组并保持布局层次结构平坦,从而提高性能。如果您发现自己使用了多个嵌套LinearLayout组,可以考虑将它们替换为单个RelativeLayout来简化布局结构。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [RelativeLayout布局](https://blog.csdn.net/qq_44610809/article/details/117815103)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【Android】相对布局RelativeLayout)最全解析](https://blog.csdn.net/huweiliyi/article/details/126448069)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [RelativeLayout相对布局详解](https://blog.csdn.net/chuyouyinghe/article/details/126960604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值