RelativeLayout的基本对齐方式

RelativeLayout的基本对齐方式在 RelativeLayo 相对布局 中 每个组件都可以通过 ID 来指定相对于其它组件或者父组件的位置 1 通过 ID 来指定相对于其它组件的位置 组件之间的相对位置关系设置如下 android layout above 将组件放在指定 ID 组件的上方 android layout below 将组件放在指定 ID 组件的下方 android layout toRig

在RelativeLayout(相对布局)中,每个组件都可以通过ID来指定相对于其它组件或者父组件的位置。

比如以下的layout_relative01.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"> <Button  android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button  android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> </RelativeLayout>

比如以下的layout_relative02.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"> <Button  android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="按钮1" ></Button> <Button  android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:layout_alignLeft="@id/button01" android:text="按钮2" ></Button> </RelativeLayout>

比如以下的layout_relative03.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"> <Button  android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="按钮1" ></Button> <Button  android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:layout_alignParentLeft="true" android:text="按钮2" ></Button> <Button  android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="按钮3" ></Button> </RelativeLayout>

比如以下的layout_relative04.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"> <Button  android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="按钮1" ></Button> <Button  android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="按钮2" ></Button> <Button  android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="按钮3" ></Button> </RelativeLayout>

5、在相对布局中,如果想固定一个组件的位置,至少要确定组件的”左右”和”上下”两个位置,才可以准确地固定组件位置。

下面的layout_relative05.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"> <Button  android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button  android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> <Button  android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:text="按钮3" ></Button> </RelativeLayout>

这时,我们想在按钮3的右边,按钮2的下方,放置一个按钮4,有以下的layout_relative06.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"> <Button  android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button  android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> <Button  android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:text="按钮3" ></Button> <Button  android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button03" android:text="按钮4" ></Button> </RelativeLayout>
<?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"> <Button  android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" ></Button> <Button  android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button01" android:text="按钮2" ></Button> <Button  android:id="@+id/button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01" android:text="按钮3" ></Button> <Button  android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/button03" android:layout_below="@id/button02" android:text="按钮4" ></Button> </RelativeLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231797.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • VS无法打开源文件

    VS无法打开源文件通过一天的时间终于弄出来了,无法找到源文件的主要原因其实就是你删了某一个文件夹,他找不到了。这是我查这么多最贴合实际的一次,其他的调的,可能也可以解决,不过会有其他问题产生,无法打开元文件。搞了半天还是不行,主要是没有从根本上下手。推荐一个链接,解决这个问题:解决无法打开源文件…

    2022年6月15日
    48
  • mac配置环境变量不生效

    mac配置环境变量不生效mac修改环境变量原理:主要原因是每次终端启动时候zsh加载的是/etc/zshrc文件,而‘zshrc’文件中并没有定义任务环境变量。cd/etc/vizshrc最后一行添加:source~/.bash_profile保存,重新打开终端即可…

    2022年6月21日
    183
  • PHP学习之一晚撸下W3chscool

    PHP学习之一晚撸下W3chscoolPHP多维数组其实简单的而言,多维数组就是由单个的数组组成的,两个数组嵌套组成一个二维数组,三个顾名思义就是三维数组。先来一个简单的数组。数字是key,引号里的是value<?php$array=array(‘1’=>”咋”,’2’=>”日”);echo$array[2];?>输出:日然后再来几个有难…

    2022年8月22日
    7
  • 排序方法

    排序方法

    2021年8月12日
    57
  • 软件实施工程师的经验之谈(适合新手,老鸟请指正)[通俗易懂]

    软件实施工程师的经验之谈(适合新手,老鸟请指正)[通俗易懂]干了三年实施,技术没学多少,人倒是变的圆滑多了问题1:实施干嘛的呢?说简单通俗点,开发就是研发生产电视机的,我们实施就是给买电视机的人去进行安装调试,试运行完了签验收单收款和后期的日常维护(当然,如果大公司有自己的售后服务团队就另当别论了)问题2:实施的薪资(我想大部分人都关注这个吧)以一线城市北上广为例,我在北京,第一份实施工作月薪4500,出差补助一天一百,报销路费和住宿费,不报销吃饭…

    2022年6月2日
    64
  • eclipse中改变默认的workspace的方法及说明

    eclipse中改变默然的workspace的方法可以有:1.在创建project的时候,手动选择使用新的workspace,如创建一个webproject,在向导中的Location选项,取消使

    2021年12月22日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号