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

android:gravity 属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.该属性就干了这个. 
android:layout_gravity是用来设置该view中的子view相对于父view的位置.比如一个button 在linearlayout里,你想把该button放在靠左,靠右等位置就可以在linearlayout中通过该属性设置.

下面看布局文件及效果图

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal" android:layout_width="fill_parent"
  4. android:layout_height="wrap_content" android:gravity="center_vertical">
  5. <ImageView android:id="@+id/ivLogo" android:layout_width="50dp"
  6. android:layout_height="50dp" android:src="@drawable/icon"
  7. android:paddingLeft="5dp" />
  8. <RelativeLayout android:id="@+id/rl_name"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" android:gravity="right"
  11. android:padding="10dp">
  12. <TextView android:id="@+id/tvApplicationName"
  13. android:layout_width="wrap_content" android:layout_height="wrap_content"
  14. android:textSize="16dp" />
  15. </RelativeLayout>
  16. <RelativeLayout android:id="@+id/rl_score"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content" android:gravity="right"
  19. android:padding="10dp">
  20. <TextView android:id="@+id/tvRating" android:layout_width="wrap_content"
  21. android:layout_height="wrap_content" android:text="5.0" />
  22. <RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"
  23. android:layout_height="wrap_content" android:numStars="5"
  24. style="?android:attr/ratingBarStyleSmall" android:layout_below="@id/tvRating" />
  25. </RelativeLayout>
  26. </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。 
这个布局虽然简单,但却是经常用到的。 
引用请注明出处:http://zhangkun716717-126-com.iteye.com/

[转]用android LinearLayout和RelativeLayout实现精确布局的更多相关文章

  1. android LinearLayout和RelativeLayout实现精确布局

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

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

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

  3. Android开发——LinearLayout和RelativeLayout的性能对比

    0. 前言 我们都知道新建一个Android项目自动生成的Xml布局文件的根节点默认是RelativeLayout,这不是IDE默认设置,而是由android-sdk\tools\templates\ ...

  4. Android 系统功能设置菜单 LinearLayout与relativeLayout的实现

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  5. Mono for Android (3)-- AbsoluteLayout、FrameLayout、LinearLayout、RelativeLayout、TableLayout

    AbsoluteLayout:允许开发人员将视图放在所定义的位置.该布局已经过时了,建议改用其他 FrameLayout:最简单的布局选项,其设计目的是在屏幕上显示单个对象.所有元素都固定在左上角.如 ...

  6. LinearLayout和RelativeLayout 区别

    LinearLayout和RelativeLayout转自:http://blog.csdn.net/w176236767/article/details/6605848共有属性:java代码中通过b ...

  7. LinearLayout和RelativeLayout

    LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件android:id="@+id/btn1" 控件宽度android:layo ...

  8. linearLayout 和 relativeLayout的属性区别(转)

    LinearLayout和RelativeLayout 共有属性:java代码中通过btn1关联次控件android:id="@+id/btn1" 控件宽度android:layo ...

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

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

随机推荐

  1. 一步一步学EF系列【6、IOC 之AutoFac】

    前言 之前的前5篇作为EF方面的基础篇,后面我们将使用MVC+EF 并且使用IOC ,Repository,UnitOfWork,DbContext来整体来学习.因为后面要用到IOC,所以本篇先单独先 ...

  2. 信息处理,分而治之-- ESFramework 使用技巧

     ESFramework开发手册系列文章已经详细介绍了如何使用ESPlus提供的ESPlus.Application.CustomizeInfo空间来发送和处理自定义信息,而且,在我们在前面介绍的de ...

  3. 关于逆波兰式的c++实现

    正常的表达式 逆波兰表达式 a+b ---> a,b,+ a+(b-c) ---> a,b,c,-,+ a+(b-c)*d ---> a,b,c,-,d,*,+ a+d*(b-c)- ...

  4. Python安装coverage.py

    coverage.py是一个用来统计python程序代码覆盖率的工具.它使用起来非常简单,并且支持最终生成界面友好的html报告.在最新版本中,还提供了分支覆盖的功能. 官方网站: http://ne ...

  5. 1010 Robot Motion

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  6. 让人头疼的CSS兼容

    先说点Hack的知识(真正的高手是不用Hack的,但要成为高手必须通过Hack这一关) /* CSS属性级Hack */ color:red; /* 所有浏览器可识别*/ _color:red; /* ...

  7. json 与entity/list/map的转换

    一  json -> entity User.java package com.xxx.hotel.train.json.json2entity; import java.io.Serializ ...

  8. Html复杂表头的实现

    实现效果 代码实现

  9. 文本注释系统 + MarkDown

    标记系统: 笔记的要点 题材 缘起 目标 等级: 细节性 事实性 规律 法则 适用范围: 时间.地点.人物.起因.经过.结果,who what where when why how whom 6W1H ...

  10. 辨别 ShopEX Ecshop

    御剑可以识别ShopEX 或者 Ecshop 特征 ShopEX : 蓝色的icon js里有很多Cookie. <link rel="stylesheet" href=&q ...