A016-布局之RelativeLayout

RelativeLayout

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

XML Attribute

XML Attribute
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"

举例

RelativeLayout
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/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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,836评论 25 709
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 47,138评论 22 665
  • @极简主义患者/社交控/伪技术宅/沉迷幻想不能自拔的文艺少年不定期更新的文字平台:微博 简书 Viewgroups...
    娄叔啊喂阅读 454评论 0 0
  • 南塘几度莲红 秋波绵绵淌过我心头 楼高望断天涯路 君可有策马归途 采莲弄莲子 木叶瑟瑟秋风中 轻敛罗绣裙 燕子别春...
    南夏有木阅读 744评论 0 0
  • 痛 是因为没走过 其实是怕多于痛 走多了也就习惯啦 祛除了怕 也就赶走了痛 小小的年纪 碰到一点疼痛 就怕 就想着...
    神于天圣于地阅读 171评论 0 1

友情链接更多精彩内容