在谈这个之前先啰嗦几个概念。
基线:书写英语单词时为了规范书写会设有四条线,从上至下第三条就是基线。基线对齐主要是为了两个控件中显示的英文单词的基线对齐,如下所示:

start:在看api的时候经常会有start对齐,end对齐,start对齐主要是为了能够在不同的textdirection(文本排列方向)的时候更好的对齐的一种方式,在textdirection为ltr(从左到右)时start对齐就等于左对齐,当textdirection为rtl(从右到左)时start对齐就等于右对齐。end同理。
通过gravity来实现view的文本对齐方式.先看下官方解释。
android:gravity:specifies how to align the text by the view‘s x-
and/or y-axis when the text is smaller than the view.
must be one
or more (separated by ‘|‘) of the following constant values.
意思是说,当文本比view小的时候指定如何通过view的x、y轴线去对齐。这个属性的值必须是下面给出的一个或多个,如果是多个则用‘|‘隔开。
可以用的属性有:<code>top</code>、<code>bottom</code>、<code>left</code>、<code>right</code>、<code>center_vertical</code>、<code>fill_vertical</code>、<code>center_horizontal</code>、<code>fill_horizontal</code>、<code>center</code>、<code>fill</code>、<code>clip_vertical</code>、<code>clip_horizontal</code>、<code>start</code>、<code>end</code>
这些属性中就谈两类,其他的类似,一个是上下左右这种基本类型的,还有一种是组合的,比如左下角,这是为:left|bottom。
左对齐:
左下角:
效果图如下所示:
gravity:specifies how an object should position its
content, on both the x and y axes, within its own bounds.
must be one or more (separated by ‘|‘) of the following constant
values.
意思是,规定了一个对象以什么样的方式根据x、y轴去摆放自己的内容。这个属性的值必须是下面给出的一个或多个,如果是多个则用‘|‘隔开。
可以用的属性有:<code>top</code>、<code>bottom</code>、<code>left</code>、<code>right</code>、<code>center_vertical</code>、<code>fill_vertical</code>、<code>center_horizontal</code>、<code>fill_horizontal</code>、<code>center</code>、<code>fill</code>、<code>clip_vertical</code>、<code>clip_horizontal</code>、<code>start</code>、<code>end。</code>
同样,这些属性中就谈两类,其他的类似,一个是上下左右这种基本类型的,还有一种是组合的,比如左上角,这是为:left|bottom。
居右对齐:
居左下角对齐:
效果图如下:
relativelayout主要用到相对位置的对齐,主要属性有以下几种
layout_above
该布局文件的底部设置到指定id的对象的上方
layout_below
layout_alignbaseline
该布局文件的基线与指定id的对象的基线对齐
layout_alignbottom
将该布局文件的底部域指定的id的对象的底部对齐
layout_alignleft
layout_alignright
layout_aligntop
layout_alignstart
该布局文件的开始(左边)与指定id对象的开始(左边)位置对齐
layout_alignend
该布局文件的结束(右边)与指定id对象的结束(右边)位置对齐
layout_toleftof
该布局文件的右边放到指定id对象的左边
类似:layout_tostartof
layout_torightof
该布局文件的左边放到指定id对象的右边
layout_centerhorizontal
如果设置为true,则将该布局文件中的自对象设置为水平居中
layout_centerinparent
layout_centervertical
layout_alignparentleft
如果设置为true,则将该布局文件的左边与其父对象的左边对齐
layout_alignparentright
layout_alignparentstart
layout_alignparenttop
layout_alignparentend
layout_alignparentbottom
拿出两个例子来简单说明下,一个是将一个button放到另一个button右边,另外一个是将一个button放到另一个button的右下角
右边:
右下角:
后记:
这次谈的内容比较粗,仅作抛砖引玉用。
原文连接:
demo下载地址: