前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >A016-布局之RelativeLayout

A016-布局之RelativeLayout

作者头像
巫山老妖
发布2018-07-20 11:50:50
4540
发布2018-07-20 11:50:50
举报
文章被收录于专栏:小巫技术博客

RelativeLayout

相对布局,如果我们想在app中实现复杂的布局,缺了它肯定不行的,前面介绍的LinearLayout适用于顺序排列的布局,然而RelativeLayout则适用于各种排列方式。

XML Attribute

上面都是我们适用RelativeLayout布局时可以用到的属性,具体我就不一个个去介绍每一个属性的用法,我们只需要知道每个View都可以根据parent和其他View来进行布局,分别四个方位:

  • top (上)
  • bottom(下)
  • left(左)
  • right(右)

eg: android:layout_alignParentTop 如果为”true”的话,表示该view的边界 对齐父视图顶部边界。

android:layout_centerVertical 如果为”true”的话,表示在父视图中竖直方向居中。

android:layout_below 定位指定View视图的顶部边界对齐指定ID视图的底部边界。 eg:android:layout_below=”@+id/button2”

android:layout_toRightOf 定位指定View视图的左边界对齐指定ID视图的右边界。 eg: android:layout_toRightOf=”@+id/button”

举例

代码语言:javascript
复制
<?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/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是按钮1" />

    <Button
        android:id="@+id/button2"
        android:layout_toRightOf="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是按钮2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_alignLeft="@+id/button2"
        android:text="我是按钮3" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是按钮4"
        android:layout_below="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:layout_toEndOf="@+id/button3" />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是按钮5"
        android:layout_alignParentRight="true"/>

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是按钮6"
        android:layout_below="@+id/button5"
        android:layout_toRightOf="@+id/button4"
        android:layout_toEndOf="@+id/button4" />
</RelativeLayout>

最后

大家可以先感受下,下面这样的一个界面应该如何搭建起来,这个是小巫参与开发的一款app,界面还算比较美观简洁,就是这样的一个界面需要我们用代码堆砌起来,我们需要把基础打牢才能做出好看的app,所以希望初学者不要急躁,多自己手写布局代码,把基本的属性设置都了解过一遍,这样搭建界面的时候才不会迷茫。

转载请注明:IT_xiao小巫 http://blog.csdn.net/wwj_748

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2015-10-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小巫技术博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • RelativeLayout
    • XML Attribute
      • 举例
        • 最后
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档