关于Android的布局

Android支持多种布局方式:

  • 线性布局(Linear Layout)
  • 相对布局(Relative Layout)
  • 表格布局(Table Layout)
  • 网格视图(Grid View)
  • 标签布局(Tab Layout)
  • 列表视图(List View)
  • 绝对布局(AbsoluteLayout)


布局都是可以嵌套使用的,比如想实现下面一个效果:


即让seekbar与edittext同处一行。可以通过嵌套一个Table Layout实现:

	<TableLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:id="@+id/tblayout">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/seekv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="false"
android:text="060"
android:background="@android:drawable/editbox_background"
android:layout_gravity="center_vertical"/>
<SeekBar
android:id="@+id/seekb"
android:max="100"
android:progress="60"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</TableRow>
</TableLayout>

说明两点:
1. android:stretchColumns="1" 该选项是一个boolean型,表示时候拉伸表格,这里设置为1,所以将表格拉伸到屏幕的宽度;为0则不拉伸。
2. android:background="@android:drawable/editbox_background" 该选项会使编辑框的显示高度变低,但“editbox_background”没找到,不知是否为系统内嵌的属性。

最常用的布局当然就是Linear Layout了,它也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类,根据定义的方向:android:orientation="horizontal/vertical",每个元素占 一行/列。其元素常用的属性有:

     android:id  —— 为控件指定相应的ID
android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
android:grivity —— 指定控件的基本位置,比如说居中,居右等位置
android:textSize —— 指定控件当中字体的大小
android:background —— 指定该控件所使用的背景色,RGB命名法
android:width —— 指定控件的宽度
android:height —— 指定控件的高度
android:padding* —— 指定控件的内边距,也就是说控件当中的内容
android:layout_weight —— 控件之间的权重比
android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示

其次常用的是Relative Layout,可以将指定的元素当着参照物然后进行排布当前的元素。其元素常用的属性有:

  android:layout_above 将该控件的底部至于给定ID的控件之上
android:layout_below 将该控件的顶部至于给定ID的控件之下
android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐
android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐
android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘
android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐
 
android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐
android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐
android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐
android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央
android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央

然后就是TableLayout了,与TableRow配合使用,每个TableRow代表一行,和HTML里的Table相似。

其它的布局说明可参见:www.cnblogs.com/csj007523/archive/2011/05/19/2051219.html

原文地址:https://www.cnblogs.com/wzc0066/p/2948213.html