线性布局的weight属性在等比例分配时比较方便,但是对复杂的界面,嵌套多层LinearLayout布局会导致渲染变慢,占用更多系统资源;而使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margin +padding就可以设置组件的显示位置。

1.容器定位

父容器定位属性示意:

兄弟容器定位属性示意:

举例:梅花布局

实现代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" > <!-- 这个是在容器中央的 -->
<ImageView
android:id="@+id/img1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:src="@drawable/pic1"/> <!-- 在中间图片的左边 -->
<ImageView
android:id="@+id/img2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_toLeftOf="@id/img1"
android:layout_centerVertical="true"
android:src="@drawable/pic2"/> <!-- 在中间图片的右边 -->
<ImageView
android:id="@+id/img3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_toRightOf="@id/img1"
android:layout_centerVertical="true"
android:src="@drawable/pic3"/> <!-- 在中间图片的上面-->
<ImageView
android:id="@+id/img4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_above="@id/img1"
android:layout_centerHorizontal="true"
android:src="@drawable/pic4"/> <!-- 在中间图片的下面 -->
<ImageView
android:id="@+id/img5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@id/img1"
android:layout_centerHorizontal="true"
android:src="@drawable/pic5"/> </RelativeLayout>

2. margin与padding的区别

margin表示组件到容器边缘距离,如:marginleft = "5dp" 表示组件距离容器左边缘5dp

padding代表的则是填充,例如:TextView设置paddingleft = "5dp",则是在组件里的元素的左边填充5dp的空间。

margin针对的是容器中的组件,而padding针对的是组件中的元素

最新文章

  1. Udp通讯(零基础)
  2. INNODB自增主键的一些问题
  3. JavaScript encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent()
  4. WPF single instance
  5. YTU 3013: 皇后问题(递归)
  6. Python中作用域的特别之处
  7. hdu1172猜数字
  8. Learning ROS for Robotics Programming Second Edition学习笔记(十) indigo Gazebo rviz slam navigation
  9. Testlink插件工具
  10. RabbitMQ跟Redis做消息队列的区别
  11. Selenium上传文件方法总结
  12. 【netcore基础】CentOS 7.6.1810 搭建.net core 2.1 linux 运行环境 nginx反向代理 supervisor配置自启动
  13. 序列化(pickle,shelve,json,configparser)
  14. 解决Android微信支付官方demo运行失败
  15. 在单链表的第i个位置后插入一个节点(阿里+腾讯等面试题总结)
  16. rabbitmq学习(五):springboot整合rabbitmq
  17. Tcp/ip实验准备:一个简单的定时器——boost实现
  18. 3.5星|《哈佛商学院最受欢迎的领导课》:讲给CEO的管理学常识、常见错误和改进方法
  19. 模板CodeTemplate
  20. C++ 友元类,友元函数

热门文章

  1. java内存模型(线程,volatile关键字和sychronized关键字)
  2. Codeforces 446C 线段树 递推Fibonacci公式
  3. javascript数组日期
  4. Maven - Eclipse例子
  5. 多源D点(邻接表+bfs)
  6. delphi的procedure of object
  7. JAVA 创建文件和文件夹,删除文件和文件夹的实用工具
  8. 触发器-- 肖敏_入门系列_数据库进阶 60、触发器(三) --youku
  9. linux文件软链接与硬链接
  10. python3转义编码