五大布局Layout
LinearLayout 线性布局
下面列出的部分常用的属性
android:orientation="horizontal"
制定线性布局的排列方式
水平 horizontal
垂直 vertical
gravity 控制当前控件内容显示区域
layout_gravity 当前控件在父元素的位置
Layout_weightSum
Layout_weight 额外空间分配(权重)
android:visibility="invisible"
控制布局是否显示
显示 visible
不显示,但占空间 invisible
隐藏 gone
RelativeLayout 相对布局
android:layout_toRightOf 在指定控件的右边
android:layout_toLeftOf 在指定控件的左边
android:layout_above 在指定控件的上边
android:layout_below 在指定控件的下边
android:layout_alignBaseline 跟指定控件水平对齐
android:layout_alignLeft 跟指定控件左对齐
android:layout_alignRight 跟指定控件右对齐
android:layout_alignTop 跟指定控件顶部对齐
android:layout_alignBottom 跟指定控件底部对齐
android:layout_alignParentLeft 是否跟父布局左对齐
android:layout_alignParentTop 是否跟父布局顶部对齐
android:layout_alignParentRight 是否跟父布局右对齐
android:layout_alignParentBottom 是否跟父布局底部对齐
android:layout_centerVertical 在父布局中垂直居中
android:layout_centerHorizontal 在父布局中水平居中
android:layout_centerInParent 在父布局中居中
AbsoluteLayout 绝对布局
AbsoluteLayout
android:layout_x 指定控件在父布局的x轴坐标
android:layout_y 指定控件在父布局的y轴坐标
TableLayout 表格布局
android:shrinkColumns 收缩列
android:stretchColumns 拉伸列
android:collapseColumns 隐藏列
android:layout_column 指定列(作用在列的身上)
android:layout_span 合并列(作用在列的身上)
TableRow单元行里的单元格的宽度小于默认的宽度时就不起作用,其默认是fill_parent,高度可以自定义大小
FrameLayout 帧布局
帧布局每次添加的控件都显示在最上面,最后显示在界面上的是最后添加的一个控件
单位的概念
- px (pixels)像素,一般HVGA代表320x480像素,这个用的比较多。
- dip或dp (device independent pixels)设备独立像素,这个和设备硬 件有关,一般为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
- sp (scaled pixels — best for text size)比例像素,主要处理字体的大小,可以根据用户系统的字体自适应。
- 除了上面三个显示单位,下面还有几个不太常用:
in (inches)英寸
mm (millimeters)毫米
pt (points)点,1/72英寸
总结:为了适应不同分辨率,不同的像素密度,推荐使用dip ,文字使用sp。
例子
这里先给出帧布局和线性布局的例子,之后会给出相对布局的例子。
FrameLayout例子
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.yzx.usualLayout.MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"/>
<TextView
android:id="@+id/textView1"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_gravity="center"
android:background="@android:color/black" />
<TextView
android:id="@+id/textView2"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_gravity="center"
android:background="@android:color/white" />
</FrameLayout>
运行结果:
LinearLayout例子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="205dip"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/white" />
<TextView
android:id="@+id/textView4"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FF0000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="205dip"
android:orientation="vertical" >
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@android:color/black" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@android:color/white" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#FF0000" />
</LinearLayout>
</LinearLayout>
运行结果: