Android学习-Layout布局

来源:互联网 发布:淘宝店铺风格定位 编辑:程序博客网 时间:2024/06/04 10:09

由于之前学习的时候还没有开始写博客,这里写一下就当做是复习一下。

LinearLayout

首先是LinearLayout里面的属性

属性名称 作用 android:layout_width 控件的宽度 android:layout_height 控件的高度 android:layout_weight 权重 android:orientation 控件在LinearLayout中的分布方式,分别是horizontal(横向)和vertical(纵向),必须指定 android:layout_gravity 此控件在父类容器中的对齐方式 android:gravity 此控件里面的内容,如里面的文字在该控件中的对齐方式 android:background 控件的背景,可以是颜色也可以是图片资源 android:layout_margin 控件相对于父容器的边距 android:padding 控件里面的内容相对于控件的边距

举个例子:

<Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="50dp"            android:text="哈哈哈"/>

效果如下
android:layout_margin

改为padding

<Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:paddingLeft="50dp"            android:text="哈哈哈"/>

效果如下
android:padding

RelativeLayout

相对布局,属性相对来说比LinearLayout多点

属性名称 作用 android:layout_alignParentTop/Right/Left/Bottom 分别是对齐父容器的顶部/右边/左边/底部,赋值类型为布尔类型 android:layout_centerInParent/Horizontal/Vertical 父类容器居中/横向居中/纵向居中,赋值类型也是布尔类型 android:layout_toRightOf 指定控件的右边,赋值为指定控件的id android:layout_toLeftOf 指定控件的左边 android:layout_above/below 指定控件的上边/下边 android:layout_alignRight/Left/Top/Bottom 对齐指定控件的右边线/左边线/上边线/下边线

举个列子:

<Button         android:id="@+id/btn_center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="中心"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@id/btn_center"        android:text="alignRight中心"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentBottom="true"        android:text="ParentRight和Bottom"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@id/btn_center"        android:layout_toRightOf="@id/btn_center"        android:text="alignTop、toRightOf中心"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@id/btn_center"        android:text="above中心"        />

效果图
这里写图片描述

TableLayout

表格布局

表格布局的行数是由TableRow来决定,列数由列数最多的那个TableRow来决定
常见的属性有

属性名称 作用 android:stretchColumns 拉伸给定的列以使组件填满tablerow(从0的开始算),填*则表示全部都可以 android:collapseColumns 隐藏给定的列 android:shrinkColumns 可以被压缩的列 android:layout_span 设置填充,比如设置成2,那么这个空间就占2列

做个列子

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TableLayout         android:layout_width="250dp"        android:layout_height="match_parent"        android:layout_centerHorizontal="true"        android:stretchColumns="1"        >       <TableRow            android:layout_height="wrap_content">           <Button                android:layout_height="wrap_content"               android:text="账户"/>           <EditText                android:layout_height="wrap_content"               android:hint="输入账号"/>       </TableRow>       <TableRow            android:layout_height="wrap_content">           <Button                android:layout_height="wrap_content"               android:text="密码"/>           <EditText                android:layout_height="wrap_content"               android:hint="输入密码"/>       </TableRow>       <TableRow            android:layout_height="wrap_content">           <Button                android:layout_height="wrap_content"               android:layout_span="2"               android:text="登录"/>       </TableRow>    </TableLayout></RelativeLayout>

效果图
这里写图片描述

这里隐藏了标题栏,只需在setContentView之前加入

requestWindowFeature(Window.FEATURE_NO_TITLE);

即可

0 0
原创粉丝点击