先明确几个概念的区别: 
padding margin:都是边距的含义,关键问题得明白是什么相对什么的边距
padding:是控件的内容相对控件的边缘的边距.

margin  :是控件边缘相对父空间的边距

android:gravity是对该view 内容的限定.

比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.该属性就干了这个.

android:layout_gravity 是用来设置该view中的子view相对于父view的位置.

比如一个button 在linearlayout里,你想把该button放在靠左,靠右等位置就可以在linearlayout中通过该属性设置

XML 布局文件

[html] view plaincopyprint?

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:gravity="center_vertical">
  7. <ImageView android:id="@+id/ivLogo"
  8. android:layout_width="50dp"
  9. android:layout_height="50dp"
  10. android:src="@drawable/icon"
  11. android:paddingLeft="5dp" />
  12. <RelativeLayout android:id="@+id/rl_name"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:gravity="right"
  16. android:padding="10dp">
  17. <TextView android:id="@+id/tvApplicationName"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:textSize="16dp" />
  21. </RelativeLayout>
  22. <RelativeLayout android:id="@+id/rl_score"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:gravity="right"
  26. android:padding="10dp">
  27. <TextView android:id="@+id/tvRating"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="5.0" />
  31. <RatingBar android:id="@+id/ratingbar"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:numStars="5"
  35. style="?android:attr/ratingBarStyleSmall"
  36. android:layout_below="@id/tvRating" />
  37. </RelativeLayout>
  38. </LinearLayout>

上面布局文件的效果图 

上面的布局文件是一个ListView中的list_item布局,在一个ListView中显示所有的APK资源,每个资源项显示图标,名称及评分。

在listItem的最外层LinearLayout中加android:gravity="center_vertical",设定内容垂直居中显示。

在id为rl_score的RelativeLayout中设定android:layout_width="fill_parent"来填充剩余空间;

android:gravity="right"设定内容相对于rl_score右对齐;

android:padding="10dp"设定RelativeLayout中的内容相对RelativeLayout的边缘的边距为10dp。 
这个布局虽然简单,但却是经常用到的。

android LinearLayout和RelativeLayout实现精确布局相关推荐

  1. 用android LinearLayout和RelativeLayout实现精确布局(转)

    先明确几个概念的区别:  padding margin都是边距的含义,关键问题得明白是什么相对什么的边距.  padding是控件的内容相对控件的边缘的边距.  margin是控件边缘相对父控件的边距 ...

  2. [转]用android LinearLayout和RelativeLayout实现精确布局

    先明确几个概念的区别:  padding margin都是边距的含义,关键问题得明白是什么相对什么的边距.  padding是控件的内容相对控件的边缘的边距.  margin是控件边缘相对父控件的边距 ...

  3. Android基础教程之五大布局对象------FrameLayout,LinearLayout,AbsoluteLayout,RelativeLayout,TableLayout...

    2019独角兽企业重金招聘Python工程师标准>>> 大家好,我们这一节讲一下Android对用五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的), ...

  4. Android 第六课 4种基本布局之LinearLayout和Relativelayout

    看完控件,紧接着看布局,布局是可以来放置控件,管理控件的.布局里也可以嵌套布局. 我们新建项目UILayoutTest项目,活动名和布局名选择默认.加入活动及其对应的布局已经创建完成. 线性布局(Li ...

  5. Android - 布局详解之LinearLayout与RelativeLayout

    本篇博文只针对LinearLayout与RelativeLayout 我们在新建一个布局文件时,一般遵循这样的思路:先确定该文件对应的界面中各个布局和控件的位置和大小,然后再来设置各个布局和控件的其他 ...

  6. Android基础教程(二)之五大布局对象---FrameLayout,LinearLayout ,AbsoluteLayout,RelativeLayout,TableLayout.

    大家好,我们这一节讲一下Android对用五大布局对象,它们分别是FrameLayout(框架布局:不知道是不是这么翻译的),LinearLayout (线性布局),AbsoluteLayout(绝对 ...

  7. Android五大布局——LinearLayout、RelativeLayout

    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLay ...

  8. Android 的LinearLayout (线性布局)和RelativeLayout (相对布局)介绍

    LinearLayout  (线性布局) 1. 在design页面下->component tree->ConstrainLayout右键->Convert view...-> ...

  9. 【Android】7.5 RelativeLayout(相对布局)

    分类:C#.Android.VS2015: 创建日期:2016-02-11 一.简介 RelativeLayout是一种相对布局,容器中子元素的位置是相对于其前一个元素或者其他元素的位置来计算的,或者 ...

最新文章

  1. 盘点 Github 上的高仿 app 项目
  2. 一文带你领略并发编程的内功心法
  3. 训练生成对抗网络的过程中,训练gan的地方为什么这里没有detach,怎么保证训练生成器的时候不会改变判别器
  4. android drawable 比例,Android中的Drawable基础与自定义Drawable
  5. Java 获取昨天、当前、明天的时间
  6. Udp---模拟实现客户端与服务器通信
  7. nginx配置文件简单理解(自用)
  8. LeetCode:3Sum_15
  9. java.lang.UnsupportedOperationException: This parser does not support specification “null“ version “
  10. excel 平滑滚动_我的Excel书——Excel职场应用实战精粹
  11. [2018.07.31 T2] 第二题
  12. clion连接mysql,使用 CLion 调试 mysql 源码
  13. 工商银行在线支付接口
  14. 个人知识管理系统思源笔记
  15. 2020年四季度混合型基金数据分析
  16. speedoffice表格中如何隐藏网络线
  17. win10安装Mysql8的两种方式(安装/卸载-图文教程)
  18. 生活中的法则II(转载)
  19. 这两个能这样搭配?海关数据+决策人挖掘,用过的外贸人都说好!
  20. 用docker-compose来搭建Hadoop(一)——创建三个ubu

热门文章

  1. java dochain,Java filter中的chain.doFilter详解
  2. css——常用选择器
  3. eclipse添加源码的另外一种方法
  4. 工作中的沟通及信息传递
  5. 【重构之法】代码的坏味道
  6. 软设考试成绩查询结果
  7. Day1 - Python基础1
  8. 动作定位、检测与理解,ICCV 2021 DeeperAction挑战赛开启~
  9. 电商标识检测的鲁棒性防御,ACM MM2021 安全AI大赛技术解析
  10. CVPR 2021 论文/代码分类汇总!持续更新中!