Android开发学习1

布局

1. 线性布局(LinearLayout)

常用属性:

  1. orientation:布局中常用的排列方式有 horizontal,Vertical两种方式
  2. gravity : 控制组件所包含的子元素的对齐方式,可多个组合,如(left | bottom)
  3. layout_gravity : 控制该组件在父容器里的对齐方式
  4. layout_width : 布局的宽度,通常不写数字,用wrap_content(组件实际大小),fill_parent或者match_parent填满父容器
    1. layout_height : 布局的高度,参数如上
    2. Id: 为该组件设置一个资源id,在java文件中可以通过findViewById(id)找到该组件。
    3. background : 为该组件设置一个背景图片,或者直接用颜色覆盖

Weight(权重):

  1. 该属性是用来等比例的划分区域
  2. 分谁,谁为0,weight按比例即可

Divider(分割线):

  1. 设置分割线图片,通过showDividers来设置分隔线的所在位置,有四个可选值 none,middle,begining,end;
  2. divider:设置分隔线图片
  3. dividerPadding:设置分隔线的padding

2. 相对布局(RelativeLayout)

基本属性

  1. gravity: 设置容器内组件的对齐方式
  2. ignoreGravity: 设置了该属性为true的组件,将不受gravity属性的影响

根据父容器定位

  1. layout_alighParentLeft : 左对齐
  2. layout_alighParentRight:右对齐
  3. layput_alighParentTop:顶部对齐
  4. layout_alighParentBottom:底部对齐
  5. layout_centerHorizontal:水平居中
  6. layout_centerVertical:垂直居中
  7. layout_centerInParent:中间位置

根据兄弟组件定位

  1. layout_toLeftOf: 参考组件的左边
  2. layout_toRightOf:参考组件的右边
  3. layout_above:参考组件的上方
  4. layout_below:参考组件的下方
  5. layout_alignBottom: 对齐参考组件的下边界
  6. layout_alignTop: 对齐参考组件的上边界
  7. layout_algnLeft:对齐参考组件的左边界
  8. layout_alignRight:对齐参考组件的右边界

margin(偏移) 设置组件与父容器的边距

  1. layout_margin:设置组件上下左右的偏移量
  2. layout_marginLeft:设置组件离左边的偏移量
  3. layout_marginRight:设置组件离右边的偏移量
  4. layout_marginTop:设置组件离上面的偏移量
  5. layout_marginButtom:设置组件离下面的偏移量

padding(填充) 设置组件内部元素间的边距(Textview里的字体位置)

  1. padding: 往内部元素的上下左右填充一定的边距
  2. paddingLeft:往内部元素的左边填充一定的边距
  3. paddingRight:往内部元素的右边填充一定的边距
  4. paddingTop:往内部元素的上方填充一定的边距
  5. paddingBottom:往内部元素的下方填充一定的边距

3. 表格布局(TableLayout)

  1. collapseColumns 隐藏列
  2. stretchColumns 拉伸列
  3. shrinkColumns(收缩列)
  4. 如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!
  5. 如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!
  6. tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定
  7. tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrapten——content的,我们却可以自己设置大小!
  8. 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
  9. 有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数
  10. android:collapseColumns:设置需要被隐藏的列的序号
    android:shrinkColumns:设置允许被收缩的列的列序号
    android:stretchColumns:设置运行被拉伸的列的列序号
    以上这三个属性的列号都是从0开始算的,比如shrinkColunmns = “2”,对应的是第三列!
    可以设置多个,用逗号隔开比如”0,2”,如果是所有列都生效,则用”*”号即可
    除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格,这和HTML中的Table类似:
    android:layout_column=”2”:表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的!
    android:layout_span=”4”:表示合并4个单元格,也就说这个组件占4个单元格
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:stretchColumns="0,3"
    android:gravity="center_vertical"
    android:background="#FF0FF0"
    >

    <TableRow>
        <TextView />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"/>
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="150dp"/>
        <TextView />
    </TableRow>

    <TableRow>
        <TextView />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密  码:"
            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="150dp"
            />
        <TextView />
    </TableRow>

    <TableRow>
        <TextView />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="退出"/>
        <TextView />
    </TableRow>

</TableLayout>

4. 帧布局(用的不多)(FrameLayout)

  1. foreground: 设置改帧布局容器的前景图像
  2. foregroundGravity: 设置前景图像显示的位置
    android:foreground="@drawable/logo"    
    android:foregroundGravity="right|bottom">

5. 网格布局(GridLayout)

设置对齐方式

  1. 设置排列方式 android:orientation=”” vertical(竖直,默认)或者 horizontal(水平)
  2. 设置对齐方式 android:layout_gravity=”” center,left,right,bottom,,,,,,,bottom|left

设置几行几列

  1. 设置行数 android:rowCount=”4” //设置网络布局有4行
  2. 设置列数 android:columnCount=”4” //设置网格布局有4列

设置组件所在的行列(从0开始计算的哦)

1.第几行 android:layout_row=”1” //设置组件位于第二行
2. 第几列 android:layout_column=”2” //设置该组件位于第三列

设置组件横跨几行几列

  1. 横跨几行:android:layout_rowSpan=”2” //纵向横跨2行
  2. 横跨几列 android:layout_columnSpan=”3”//横向纵跨3列

6. 绝对布局(AbsoluteLayout)直接拖动控件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值