一、LinearLayout布局控件

xml属性

android:baselineAligned:是否允许用户调整它内容的基线。

android:baselineAlignedChildIndex:当一个线性布局与另一个布局是按基线对齐的一部分,它可以指定其内容的基线对齐方式。

android:gravity:指定控件中内容的基本内容。

android:orientation:设置它内容的对其方向,有两个可以选择的值:horizontal和vertical。分别表示水平排列和垂直排列。

LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失,不能完全显示。因此垂直方式排列时,每一行只会有一个widget或者是container,而不管他们有多宽,而水平方式排列是将会只有一个行高(高度为最高子控件的高度加上边框高度)LinearLayout保持其所包含的widget或者是container之间的间隔以及互相对齐(相对一个控件的右对齐、中间对齐或者左对齐)

LinearLayout还支持为其包含的widget或者是container指定填充权值。允许其包含的widget或者是container可以填充屏幕上的剩余空间。剩余的空间会按这些widgets或者是containers指定的权值比例分配屏幕。默认的 weight 值为0,表示按照widgets或者是containers实际大小来显示,若高于0的值,则将Container剩余可用空间分割,分割大小具体取决于每一个widget或者是containerlayout_weight及该权值在所有widgets或者是containers中的比例。例如,如果有三个文本框,前两个文本框的取值一个为2,一个为1,显示第三个文本框后剩余的空间的2/3给权值为2的,1/3大小给权值为1的。而第三个文本框不会放大,按实际大小来显示。也就是权值越大,重要度越大,显示时所占的剩余空间越大。

LinearLayout示例:

布局文件:

<?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="fill_parent" > <!-- android:id ———————————— 为控件指定id android:text —————————— 指定控件当中显示的文字 android:gravity ——————— 指定控件中内容的基本位置,比如居中,靠右等位置 android:textSize —————— 指定控件当中字体的大小 android:background ———— 指定该控件所使用的背景色,RGB命名法 android:width ————————— 指定控件的宽度 android:height ———————— 指定控件的高度 android:padding ——————— 指定控件的四个方向的内边距 android:paddingLeft———— 指定控件的左内边距 android:paddingRight——— 指定控件的右内边距 android:paddingTop ———— 指定控件的上内边距 android:paddingBottom — 指定控件的底内边距 android:sigleLine ————— 如果设置为真的话,则将控件中的内容在一行进行显示 android:layout_weight — 设置在LinearLayout控件中的填充权值 --> <TextView android:id="@+id/first" android:layout_width="200dip" android:layout_height="200dip" android:text="第一个TextView" android:textSize="20pt" android:gravity="center_vertical" android:background="#aa0000" android:paddingLeft="10dip" android:paddingRight="30dip" android:paddingTop="10dip" android:paddingBottom="30dip" android:layout_weight="2" /> <TextView android:id="@+id/second" android:layout_width="200dip" android:layout_height="200dip" android:text="第二个TextView" android:textSize="20pt" android:gravity="center_vertical" android:background="#0000aa" android:paddingLeft="10dip" android:paddingRight="30dip" android:paddingTop="10dip" android:paddingBottom="30dip" android:layout_weight="1" /> </LinearLayout>

显示效果:

TableLayout布局控件

TableLayout没有边框的,它是由多个TableRow对象组成,每个TableRow可以有0个或多个单元格,每个单元格就是一个View。这些TableRow,单元格不能设置layout_width,宽度默认是fill_parent的,只有高度layout_height可以自定义,默认是wrap_content。

android:shrinkColumns设置可以收缩的列号,android:stretchColumns设置可以伸展的列号。需要注意的是列号从0开始,也可以用”*”表示指定所有的列。

TableLayout中的最大列数是所有的位于TableLayout中的TableRow的列数中的最大值,即列数最多的一行有多少列,TableLayout就有多少列。

TableLayout示例:

xml布局文件:

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:text="row1_column1" android:padding="3dip" android:background="#0000aa" /> <TextView android:text="row1_column2" android:padding="3dip" android:background="#00aa00" android:gravity="center_horizontal" /> <TextView android:text="row1_column3" android:gravity="right" android:padding="3dip" android:background="#aa0000" /> </TableRow> <TableRow> <TextView android:text="row2_column1" android:padding="3dip" android:background="#aa0000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="row1_column2" android:padding="3dip" android:background="#0000aa" /> </TableRow> </TableLayout>

显示效果:第1列伸展,以充满整个屏幕。

Android常见布局控件之LinearLayout和TableLayout_第1张图片

更多相关文章

  1. Android开发——控件基础(三)EditText组件以及简单的登录界面
  2. UI布局参数(持续更新)
  3. Android ConstraintLayout 约束布局详解
  4. Android项目打包、Eclipse视图和UI控件
  5. Android中的布局属性

随机推荐

  1. ANDROID EMULATOR 4 启动报错
  2. Android SDK API 15 各安装包下载地址
  3. Android TextView使用权重时文字无法居中
  4. 2.4.12 画廊视图
  5. Android之Animation属性配置文件
  6. ch023 Android ContentProvider(第一部分
  7. Android配置文件中标签
  8. Android单元测试
  9. android全屏显示
  10. Android多点触控