Android布局中涉及的一些属性
Android:gravity属性
线性布局常见的就是利用LinearLayout进行布局,其中有个比较重要的属性就是android:gravity,在官方文档中是这么描述这个属性的:指定一个元素怎么放置它的内容,包括在X和Y轴,在它自己的边框中。
下面我们将在一个简单的TextView中应用android:gravity属性。假设我们想要TextView内的内容在右侧显示,那么我们就可以编写对应的XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_medium"
android:background="#ffffff"
android:gravity="right"
android:text="@string/hello_world"
android:textColor="#ff0000"
android:textSize="@dimen/font_size" />
</LinearLayout>
效果如下图

盒模型
为了更加准确地控制TextView里面内容的位置,我们可以使用一系列的padding属性来控制。在使用padding属性之前,先科普一下padding和Marigin之间的区别,然后我们在通过实际的效果看看他们之间的差异。
下图所示是一个类似盒子的模型,我们将通过下面的模型来讲解Padding和Marigin之间的区别。从图中可以看出,在Container(父控件)里面有一个子控件,假设是一个TextView控件。其中Margin是子控件与父控件之间的间隔大小。Border是子控件的边框,它是子控件和父控件的边界。Padding是指子控件中的内容(Content Area)与子控件Border的间隔大小。

margin属性
Android中有一系列的margin属性,下面让我们看看其中的android:layout_marginRight属性,为了有一个对比的效果,我们先将marginRight设为0dip,再将其设为50dip,如以下两图所示
|
android:layout_marginRight="0dip" |
android:layout_marginRight="50dip" |
|
|
|
从上图中,我们可以看出,左图TextView控件跟他的父控件的是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。
与marginRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。
| 属性名 | 相关方法 | 描述 |
| android:layout_marginBottom | setMargins(int,int,int,int) | Specifies extra space on the bottom side of this view. |
| android:layout_marginEnd | setMarginEnd(int) | Specifies extra space on the end side of this view. |
| android:layout_marginLeft | setMargins(int,int,int,int) | Specifies extra space on the left side of this view. |
| android:layout_marginRight | setMargins(int,int,int,int) | Specifies extra space on the right side of this view. |
| android:layout_marginStart | setMarginStart(int) | Specifies extra space on the start side of this view. |
| android:layout_marginTop | setMargins(int,int,int,int) | Specifies extra space on the top side of this view. |
padding属性
下面让我们来看看android:layout_paddingRight属性。我们将在以下布局中,通过改变android:layout_paddingRight属性,来看看变化。

为了有一个对比的效果,我们先将paddingRight设为0dip,再将其设为50dip,如以下两图所示
| android:layout_paddingRight="0dip" | android:layout_paddingRight="50dip" |
![]() |
![]() |
从上图中,我们可以看出,左图TextView控件中的内容跟TextView的边框(border)是没有右间隔的,而右图明显的有一块间隔(见右图黄色圈圈部分)。
与paddingRight相同的还有以下属性,它们的原理都相同,就不一一细讲了。
| 属性名 | 相关方法 | 描述 |
| android:padding | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of all four edges. |
| android:paddingBottom | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the bottom edge; see padding. |
| android:paddingEnd | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the end edge; see padding. |
| android:paddingLeft | setPadding(int,int,int,int) | Sets the padding, in pixels, of the left edge; see padding. |
| android:paddingRight | setPadding(int,int,int,int) | Sets the padding, in pixels, of the right edge; see padding. |
| android:paddingStart | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the start edge; see padding. |
| android:paddingTop | setPaddingRelative(int,int,int,int) | Sets the padding, in pixels, of the top edge; see padding. |
Android布局中涉及的一些属性的更多相关文章
- android布局中使用include及需注意点
在android布局中,使用include,将另一个xml文件引入,可作为布局的一部分,但在使用include时,需注意以下问题: 一.使用include引入 如现有标题栏布局block_header ...
- Android布局中的空格以及占一个汉字宽度的空格的实现
在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: 窄空格: 一个汉字宽度的空格: [用两个空格( )占一个汉字的宽度时,两个空格比 ...
- Android布局中的空格以及占一个汉字宽度的空格,实现不同汉字字数对齐
前言 在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: (普通的英文半角空格但不换行) 窄空格: (中文全角空格 (一个中文宽度)) ...
- Android布局中的layout_weight和weightSum属性的详解及使用
由于Android设备的尺寸大小不一,种类繁多,当我们在开发应用的时候就要考虑屏幕的适配型了,尽可能让我们的应用适用于主流机型的尺寸,这样我们的应用不会因为尺寸不同而不美观,解决屏幕适配问题的方法有很 ...
- Android 布局中 如何使控件居中
首先要分两种不同情况,在两种不同的布局方式下:LinearLayout 和RelativeLayout 1. LinearLayout a). android:layout_gravity=" ...
- 【转】在Android布局中使用include和merge标签
内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...
- Android布局中 android:layout_gravity="bottom"为何不起作用?
在android布局时我们有时会需要将位于LinearLayout布局中的控件放在布局底部,或者是同时想将几个控件底部对齐,此时我们自然会想到使用 android:layout_gravity=&qu ...
- android 布局中 layout_gravity、gravity、orientation、layout_weight
线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...
- android布局中显示隐藏动画
android 在布局中提供属性,能简单的加入动画效果,例如以下: <LinearLayout ... animateLayoutChanges="true" ... /&g ...
随机推荐
- Java系列笔记(6) - 并发(上)
目录 1,基本概念 2,volatile 3,atom 4,ThreadLocal 5,CountDownLatch和CyclicBarrier 6,信号量 7,Condition 8,Exchang ...
- Karma +Jasmine+ require JS进行单元测试并生成测试报告、代码覆盖率报告
1. 关于Karma Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner). 该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuou ...
- sqlserver表数据导出为insert into语句
<1>select 'insert into table_name (name,code) values ('''+name+''','''+code+''');' sql_str fr ...
- jquery.fileupload插件的简易使用日志
来源:http://yixiandave.iteye.com/blog/1897330 本文内容主要包含fileupload插件传递参数和取回服务器返回数据的方法 jquery.fileupload官 ...
- centos 7 u盘安装
centos 7 u盘安装 http://www.centoscn.com/image-text/setup/2014/0724/3342.html
- P2001xor-sigma 字典树,然而好坑
https://vijos.org/p/2001 设perXor[i]表示1---i的前缀异或值. 那么要得到某一段的异或值,只需要perXor[j] ^ perXor[i - 1] 那么我们把per ...
- 反射类属性生成DataTable
public class People //类名 { private static string name; //字段 private string sex;//字段 public string Se ...
- Python学习笔记第七天(列表解析)
列表解析: 根据已有列表,高效生成列表的方式,它的执行效率要快很多,比for循环的速度要快很多 示例如下: 生成器表达式 生成器表达式并不真正创建数字列表,而是返回一个生成器对象,此对象在每次计算出一 ...
- 远程桌面Default.rdp 中各个参数的含义(转)
存储在 Default.rdp 文件中的设置 默认情况下,将在“我的文档”文件夹中创建 Default.rdp 文件.以下 RDP 设置存储在 Desktop.rdp 文件中: desktopwidt ...
- Git常用命令(自己总是忘记,整理在这里)
1.git init 初始化一个空的git仓库 2.git clone +SSH地址 clone新的项目到本地 3.git add git add file 4.git commi ...



