为了账号安全,请及时绑定邮箱和手机立即绑定

Android零基础入门第28节:轻松掌握RelativeLayout相对布局

标签:
Android

在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局。但在实际开发中使用LinearLayout远远不够,我们本期一起来学习RelativeLayout。

                                              

一、认识RelativeLayout

RelativeLayout,又叫相对布局,使用RelativeLayout标签。相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。

下表显示了RelativeLayout支持的常用XML属性及相关方法的说明。为了控制该布局容器中各子组件的布局分布,RelativeLayout提供了一个内部类: RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局容器中子组件的布局分布。

在相对于容器定位的属性主要有以下几个,属性值为true或false。

·         android:layout_centerHorizontal:控制该组件是否和布局容器的水平居中。

·         android:layout_centerVertical:控制该组件是否和布局容器的垂直居中。

·         android:layout_centerInparent:控制该组件是否和布局容器的中央位置。

·         android:layout_alignParentTop:控制该组件是否和布局容器的顶部对齐。

·         android:layout_alignParentBottom:控制该组件是否和布局容器的底端对齐。

·         android:layout_alignParentLeft:控制该组件是否和布局容器的左边对齐。

·         android:layout_alignParentRight:控制该组件是否和布局容器的右边对齐。

·         android:layout_alignParentStart:控制该组件是否和布局容器的开始对齐。

·         android:layout_alignParentEnd:控制该组件是否和布局容器的末端对齐。

·         android:layout_alignWithParentIfMissing:如果对应的兄弟组件找不到的话就以父容器做参照物。

在相对于其他组件定位的属性主要有以下几个,属性值为其他组件的id。

·         android:layout_toLeftOf:本组件在某组件的左边。

·         android:layout_toRightOf:本组件在某组件的右边。

·         android:layout_toStartOf:本组件在某组件开始端。

·         android:layout_toEndOf:本组件在某组件末端。

·         android:layout_above:本组件在某组件的上方。

·         android:layout_below:本组件在某组件的下方。

·         android:layout_alignBaseline:本组件和某组件的基线对齐。

·         android:layout_alignTop:本组件的顶部和某组件的的顶部对齐。

·         android:layout_alignBottom:本组件的下边缘和某组件的的下边缘对齐。

·         android:layout_alignRight:本组件的右边缘和某组件的的右边缘对齐。

·         android:layout_alignLeft:本组件左边缘和某组件左边缘对齐。

·         android:layout_alignStart:本组件的开始端和某组件开始端对齐。

·         android:layout_alignEnd:本组件的末端和某组件末端对齐。

除此之外,RelativeLayout.LayoutParams 还继承了 android view. ViewGroup.MarginLayoutParams,因此 RelativeLayout 布局容器中每个子组件也可指定 android.view.ViewGroiip.MarginLayoutParams所支持的各XML属性。

二、示例

接下来通过一个简单的示例程序来学习RelativeLayout的使用用法。

继续使用app/main/res/layout/目录下的activity_main.xml文件,在其中填充如下代码片段:

[代码]xml代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

<?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:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="容器左上侧"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"/>

    <!-- 定义该组件位于父容器上侧水平居中 -->

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="容器上侧水平居中"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"/>

    <!-- 定义该组件位于父容器右上侧 -->

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="容器右上侧"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"/>

    <!-- 定义该组件位于父容器左侧垂直居中 -->

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="左中"

        android:layout_alignParentLeft="true"

        android:layout_centerVertical="true"/>

    <!-- 定义该组件位于父容器右侧垂直居中 -->

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="右中"

        android:layout_alignParentRight="true"

        android:layout_centerVertical="true"/>

    <!-- 定义该组件位于父容器左下侧 -->

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="容器左下侧"

        android:layout_alignParentLeft="true"

        android:layout_alignParentBottom="true"/>

    <!-- 定义该组件位于父容器下侧水平居中 -->

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="容器下侧水平居中"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"/>

    <!-- 定义该组件位于父容器右下侧 -->

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="容器右下侧"

        android:layout_alignParentRight="true"

        android:layout_alignParentBottom="true"/>

 

 

    <!-- 定义该组件位于父容器中间 -->

    <Button

        android:id="@+id/center_btn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="容器中央"

        android:layout_centerInParent="true"/>

    <!-- 定义该组件位于center_btn组件的上方 -->

    <Button

        android:id="@+id/center_top_btn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="中上"

        android:layout_above="@id/center_btn"

        android:layout_alignLeft="@id/center_btn"/>

    <!-- 定义该组件位于center_btn组件的下方 -->

    <Button

        android:id="@+id/center_bottom_btn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="中下"

        android:layout_below="@id/center_btn"

        android:layout_alignLeft="@id/center_btn"/>

    <!-- 定义该组件位于center_btn组件的左边 -->

    <Button

        android:id="@+id/center_bottom_left_btn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="中下左"

        android:layout_toLeftOf="@id/center_btn"

        android:layout_alignTop="@id/center_bottom_btn"/>

    <!-- 定义该组件位于center_btn组件的右边 -->

    <Button

        android:id="@+id/center_top_right_btn"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="中上右"

        android:layout_toRightOf="@id/center_btn"

        android:layout_alignTop="@id/center_top_btn"/>

</RelativeLayout>

运行程序,可以看到界面效果:

到此,RelativeLayout的示例结束,关于RelativeLayout的更多用法可以参照上面的XML属性和方法参照表,建议多动手练习。

今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

原文链接:http://www.apkbus.com/blog-205190-68553.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消