Android修行手册 - RelativeLayout属性和示例

1,290 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第23天,点击查看活动详情

👉关于作者

众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!

专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)

欢迎关注公众号【空名先生】获取更多资源和交流!

👉前提

这是小空坚持写的Android新手向系列,欢迎品尝。

新手(√√√)

大佬(√)

👉实践过程

Hello,大家好啊,我是小空,今天带大家学习常用布局RelativeLayout。

前面我们学习过了LinearLayout,这俩搭配干活能实现大部分效果。

我们知道LinearLayout是线性布局,是个那种四四方方规规矩矩的样式,而UI给出的效果往往古里古怪,单纯的线性布局肯定不满足,所以相对布局就出来了,她就像一张空白的纸,你可以在任意地方绘制。

如果复杂布局,想要用LinearLayout实现,就需要多层嵌套,这必然会降低渲染效率。

😜主要属性

  • android:gravity:设置里面view的排序方式,是中心居中,还是垂直居中水平居中,上下左右对齐
  • android:ignoreGravity:如果设置改属性为 true,将忽略 android:gravity,挺废物的属性,不用不写gravity属性不就行了
  • android:layout_alignParentStart和android:layout_alignParentLeft:设置该View左对齐父RelativeLayout
  • android:layout_alignParentEnd和android:layout_alignParentRight:设置该View右对齐父RelativeLayout
  • android:layout_alignParentTop:设置该View顶部对齐父RelativeLayout
  • android:layout_alignParentBottom:设置该View底部对齐父RelativeLayout
  • android:layout_centerHorizontal:设置该View在父RelativeLayout中水平居中
  • android:layout_centerVertical:设置该View在父RelativeLayout中垂直居中
  • android:layout_centerInParent:设置该View在父RelativeLayout的居中位置
  • android:layout_toStartOf和android:layout_toLeftOf:传入的是目标view的id,表示紧贴目标组件的左边
  • android:layout_toEndOf和android:layout_toRightOf:传入的是目标view的id,表示紧贴目标组件的右边
  • android:layout_above:传入的是目标view的id,表示紧贴目标组件的上方
  • android:layout_below:传入的是目标view的id,表示紧贴目标组件的下方
  • android:layout_alignTop:传入的是目标view的id,表示对齐目标组件的上边界
  • android:layout_alignBottom:传入的是目标view的id,表示对齐目标组件的下边界
  • android:layout_alignStart和android:layout_alignLeft:传入的是目标view的id,表示对齐目标组件的左边界
  • android:layout_alignEnd和android:layout_alignRight:传入的是目标view的id,表示对齐目标组件的右边界
  • android:padding:设置组件上下左右的填充,可以为负数,常用属性
  • android:paddingStart和android:paddingLeft:设置组件左边的填充,可以为负数,常用属性
  • android:paddingTop:设置组件上边的填充,可以为负数,常用属性
  • android:paddingEnd和android:paddingRight:设置组件右边的填充,可以为负数,常用属性
  • android:paddingBottom:设置组件下边的填充,可以为负数,常用属性
  • android:layout_margin:设置组件上下左右的偏移量,可以为负数,常用属性
  • android:layout_marginStart和android:layout_marginLeft:设置组件左边的偏移量,可以为负数,常用属性
  • android:layout_marginTop:设置组件上边的偏移量,可以为负数,常用属性
  • android:layout_marginEnd和android:layout_marginRight:设置组件右边的偏移量,可以为负数,常用属性
  • android:layout_marginBottom:设置组件下边的偏移量,可以为负数,常用属性

image.png

😜示例

<?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">
    <!-- 左上角 -->
    <ImageView
        android:id="@+id/leftTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/center"
        android:layout_toStartOf="@id/center"
        android:padding="2dp"
        android:src="@mipmap/dragon_zero" />
    <!-- 在中间图片的上面-->
    <ImageView
        android:id="@+id/top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/center"
        android:layout_centerHorizontal="true"
        android:padding="2dp"
        android:src="@mipmap/dragon_one" />
    <!-- 右上角 -->
    <ImageView
        android:id="@+id/rightTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/center"
        android:layout_toEndOf="@id/center"
        android:padding="2dp"
        android:src="@mipmap/dragon_two" />
    <!-- 在中间图片的左边 -->
    <ImageView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@id/center"
        android:padding="2dp"
        android:src="@mipmap/dragon_three" />

    <ImageView
        android:id="@+id/center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="2dp"
        android:src="@mipmap/dragon_four" />
    <!-- 在中间图片的右边 -->
    <ImageView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/center"
        android:padding="2dp"
        android:src="@mipmap/dragon_five" />
    <!-- 左下角 -->
    <ImageView
        android:id="@+id/leftBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/center"
        android:layout_toStartOf="@id/center"
        android:padding="2dp"
        android:src="@mipmap/dragon_six" />
    <!-- 在中间图片的下面 -->
    <ImageView
        android:id="@+id/bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/center"
        android:layout_centerHorizontal="true"
        android:padding="2dp"
        android:src="@mipmap/dragon_seven" />

    <!-- 右下角 -->
    <ImageView
        android:id="@+id/rightBottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/center"
        android:layout_toEndOf="@id/center"
        android:padding="2dp"
        android:src="@mipmap/dragon_eight" />
</RelativeLayout>

image.png

👉其他

📢作者:小空和小芝中的小空

📢转载说明-务必注明来源:芝麻粒儿 的个人主页 - 专栏 - 掘金 (juejin.cn)

📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。