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 ... 
随机推荐
- XidianOJ 1112 Too stupid
			题目描述 某天 light由于太富而且太帅遭到了歹徒的袭击,现在他遇到了n个歹徒,准备对light施行不法行为,虽然light身体强壮,但是毕竟只有一个人肯定打不过那么多歹徒,但是高智商的light觉 ... 
- linux查看访问windows共享目录NT_STATUS_DUPLICATE_NAME问题解决
			linux查看访问windows共享目录NT_STATUS_DUPLICATE_NAME问题解决 [jason@superfreak ~]$ smbclient //powerhouse-smb.my ... 
- python学习笔记3-celery分布式任务处理器
			celery是用python写的一个异步的任务框架,功能非常强大,具体的说明可以查看官网,这里主要提供点demo让你迅速使用该框架 1.环境安装 默认安装好了redis pip install c ... 
- GridView多列排序
			public class WebGridView:GridView { 属性#region 属性 /**//// <summary> /// 是否启用或者禁止多列排序 /// </s ... 
- C++STL学习笔记_(1)string知识
			/*============================================ string是STL的字符串类型,通常用来表示字符串 = ======================== ... 
- Selenium2+python自动化25-js处理日历控件(修改readonly属性)
			前言 日历控件是web网站上经常会遇到的一个场景,有些输入框是可以直接输入日期的,有些不能,以我们经常抢票的12306网站为例,详细讲解如何解决日历控件为readonly属性的问题. 基本思路:先用j ... 
- mfc中Button、Edit Control和MFC EditBrowse Control的用法
			[前(fei)言(hua)] 写LL(1)分析器被CString转string卡了一个多小时也是醉了. 趁着还算清醒写下这次用到的控件的使用方法好了. 这次实验的mfc用到了四个控件:Edit Con ... 
- vs代码段快捷键设置
			1 工具->代码段管理器 
- iOS UILabel根据字符串长度自动适应宽度和高度
			//这个frame是初设的,没关系,后面还会重新设置其size. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0, ... 
- oracle 中文乱码---查看和修改客户端字符集
			客户端NLS_LANG的设置方法 Windows: # 常用中文字符集set NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK # 常用unicode字符集 set ... 
 
			
		
