详解四种基本布局

来源:互联网 发布:php 身份证号码 编辑:程序博客网 时间:2024/06/13 22:48

1.  LinearLayout

         LinearLayout又称作线性布局,是一种非常常用的布局。
1.1  android:orientation属性指定了排列方向
          android:orientation="vertical"            指定了排列方向是垂直方向
          android:orientation="horizontal"       指定了排列方向是水平方向(默认)
       注意:如果LinearLayout的排列方向是horizontal,内部的控件就绝对不能将宽度指定为match_parent,因为这样的话单独一个控件就会将整个水平方向占满,其他的控件就没有可放置的位置了。同理,如果LinearLayout的排列方向是vertical,内部的控件就不能将高度指定为match_parent。

1.2  android:layout_gravity属性用于指定控件在布局中的对齐方式,而android:gravity属性用于指定文字在控件中的对齐方式
       注意:当LinearLayout的排列方向是horizontal时,只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同理,当LinearLayout的排列方向是vertical时,只有水平方向上的对齐方式才会生效。

1.3  android:layout_weight属性允许我们使用比例的方式来指定控件的大小
      注意: 我们使用了android:layout_weight属性,此时控件的宽度就不应该再由android:layout_width来决定,我们此时一般写成android:layout_weight="0dp"

2.  RelativeLayout

     RelativeLayout又称作相对布局,和LinearLayout的排列规则不同,RelativeLayout显得更加随意一些,它可以通过相对定位的方式让控件出现在布局的任何位置。

android:layout_alignParentTop                 与父布局的顶边对齐

android:layout_alignParentLeft                 与父布局的左边对齐

android:layout_alignParentRight              与父布局的右边对齐

android:layout_alignParentBottom           与父布局的底边对齐

android:layout_centerInParent                  与父布局的中间对齐


android:layout_above="@id/button1"                   让这个控件位于另一个控件(button1)的上方

android:layout_below="@id/button1"                   让这个控件位于另一个控件(button1)的下方

android:layout_toLeftOf="@id/button1"               让这个控件位于另一个控件(button1)的左侧

android:layout_toRightOf="@id/button1"             让这个控件位于另一个控件(button1)的右侧


android:layout_alignLeft="@id/button1"               让这个控件的左边缘和另一个控件(button1)的左边缘对齐

android:layout_alignRight="@id/button1"             让这个控件的右边缘和另一个控件(button1)的右边缘对齐

android:layout_alignTop="@id/button1"                让这个控件的上边缘和另一个控件(button1)的上边缘对齐

android:layout_alignBottom="@id/button1"          让这个控件的下边缘和另一个控件(button1)的下边缘对齐

3.  FrameLayout

这种布局没有任何的定位方式,所有的控件都会摆放在布局的左上角,并且后面添加的控件会压在前面添加的控件的上面。

4. TableLayout

在设计表格时,尽量让每一行都拥有相同的列数,当表格的某行一定要有不相等的列数时,就要通过合并单元格的方式来解决

注意:TableRow中无法指定控件的宽度

android:inputType=“textPassword”    密码输入框

android:layout_span="2"           合并两列的单元格

android:stretchColumns="1"    表示如果表格不能完全占满屏幕宽度,就将第二列进行拉伸,如果指定为0就是拉伸第一列


   




1 0