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