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

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

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

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

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

XML 布局文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"> <ImageView android:id="@+id/ivLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/icon"
android:paddingLeft="5dp" /> <RelativeLayout android:id="@+id/rl_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dp"> <TextView android:id="@+id/tvApplicationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp" />
</RelativeLayout> <RelativeLayout android:id="@+id/rl_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="10dp"> <TextView android:id="@+id/tvRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5.0" /> <RatingBar android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:layout_below="@id/tvRating" />
</RelativeLayout> </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是控件边缘相对父控件的边距. a ...

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

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

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

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

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

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

  5. Android五大布局详解——RelativeLayout(相对布局)

    RelativeLayout 接着上一篇,本篇我将介绍RelativeLayout(相对布局)的一些知识点. RelativeLayout 这是一个非常常用的布局,相比于上节所学到的LinearLay ...

  6. Android的学习第六章(布局二--RelativeLayout)

    今天我们来说一下Android布局中的Relativelayout布局(相对布局) 根据英译过来意思是相对布局,很容易理解,这一样布局使用的是元素与元素之间的微调做到布局的 含义:通过元素与元素之间的 ...

  7. Android的学习第六章(布局一LinearLayout)

    今天我们来说一下Android五大布局-LinearLayout布局(线性布局) 含义:线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的, 主要作用:主要对整个界面 ...

  8. 关于android LinearLayout的比例布局(转载)

    关于android LinearLayout的比例布局,主要有以下三个属性需要设置: 1,android:layout_width,android:layout_height,android:layo ...

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

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

随机推荐

  1. 53个Oracle语句优化规则详解(转)

    Oracle sql 性能优化调整  1. 选用适合的ORACLE优化器        ORACLE的优化器共有3种:a. RULE (基于规则)   b. COST (基于成本) c. CHOOSE ...

  2. UITableViewCell的4种样式

    转自http://blog.csdn.net/crazyzhang1990/article/details/12503163 1.UITableViewCellStyleDefault: Defaul ...

  3. iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...

  4. isalpha函数,判断字符是否是字母

    头文件:<iostream> or  <cctype>  在c语言中<ctype.h> 功能:判断一个字符是否是英文字符,是大写返回1,是小写返回2,不是英文字符返 ...

  5. 关于 android Intent 传对象和对象数组的一些操作

    直接开正题,Intent传递值就是平常那些很简单的,接下来介绍传递 对象,和 对象数组 1 .intent 传递自定义的 对象 - 实体类继承  Serializable public class A ...

  6. Enum基础

    enum ColorE {     RED, GREEN, BLUE; } public class GetEnumContent {     public static void main(Stri ...

  7. Delphi 2010下载+完美破解

    点击链接进入http://altd.embarcadero.com/download/RADStudio2010/delphicbuilder_2010_3615_win.isoRAD Studio/ ...

  8. SQL 建表 插数据

    CREATE TABLE `article` ( `id` INT() NOT NULL PRIMARY KEY auto_increment, `title` CHAR() NOT NULL, `c ...

  9. (转)C++笔记:面向对象编程基础

    非常棒的总结 http://blog.csdn.net/liufei_learning/article/details/21312701 面向对象编程基础 面向对象编程基于三个基本概念: 数据抽象-类 ...

  10. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...