Android五种布局

来源:互联网 发布:全国网络教育统考网 编辑:程序博客网 时间:2024/05/21 10:54

1. LinearLayout

LinearLayout是线性布局控件,它包含的子控件将以横向或纵向排列。

注:布局之间可以嵌套使用。即布局内既可包含控件,又可包含布局。

两个全局属性:

1. android:orientation --决定其子类控件排布方式android:orientation="horizontal" --水平排布android:orientation="vertical" --垂直排布2. android:gravity --决定子类控件位置android:gravity="bottom" --底部android:gravity="bottom|center_horizontal"--标签可连用(注:竖线左右不能加空格,需要注意逻辑)--gravity几个属性值:center_horizontal  --水平居中center_vertical  --垂直居中center  --水平垂直都居中right  --子类控件位于当前布局的右边left  --子类控件位于当前布局的左边bottom  --底部

子控件常用属性:

1. android:layout_gravity= "bottom"  --控件本身在当期父容器的位置2. android:layout_weight= "n"  --指本身控件占当前父容器的一个比例(n为数字)

weight属性示例代码:

<EditText     android:id="@+id/text_1"    android:layout_width="0dp" --注意此处为0dp(勿忘单位)    android:layout_weight="1" --weight    android:layout_height="wrap_content" /><Button    android:id="@+id/button1"    android:layout_width="0dp" --注意此处为0dp(勿忘单位)    android:layout_height="wrap_content"    android:layout_weight="1" --weight    android:text="send" />

注意:两个控件的layout_width属性均为”0dp”,也说明有layout_weight属性的时候,layout_width属性不起作用。而此处指定为0是一种比较规范的写法。

效果如图所示:
weight属性

注:系统会先把 LinearLayout下所有控件指定的layout_weight 相加,得到一个总值,然后每个控件所占大小的比例就是用该控件的layout_weight 值除以刚才算出的总值。

若代码改为:

<EditText     android:id="@+id/text_1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_weight="1" /><Button    android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="send" />

则效果如下图所示:
weight属性
这样看起来更加舒服,而且在各种屏幕上的适配效果会更好。

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

2. RelativeLayout

RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列。
子类控件相对父类容器:

android:layout_margin="40dp" --子控件距父容器的边距android:layout_marginLeft="32dp" --子控件距父容器的左边距android:layout_marginTop="40dp" --子控件距父容器的顶边距android:layout_alignParentLeft="true" --子控件相对当前父容器靠左边android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true" --子控件相对父容器水平居中android:layout_centerVertical="true"android:layout_centerInParent="true" --水平、垂直都居中

子类控件相对子类控件:

android:layout_below="@+id/bt1" --给定标签的底部android:layout_above="@+id/bt1" --上面android:layout_toRightOf="@+id/bt1" --右边android:layout_alignBaseline="@+id/bt1" --该控件与给定控件内容在一条线上android:layout_alignTop="@+id/bt1" --该控件与给定控件顶部对齐android:layout_alignBottom="@+id/bt1"android:layout_alignLeft="@+id/bt1" --该控件与给定控件左边缘对齐android:layout_alignRight="@+id/bt1"

3. FrameLayout

FrameLayout是帧布局中,所有子元素都不能被放置指定位置,默认放在布局左上角。且后面的子元素直接覆盖在前面的子元素之上,可将前面的子元素部分或全部遮挡。
如图所示:
FrameLayout_1 FrameLayout_2

几个属性:

android:foreground="" --设置前景图 ,在所有子视图的前面android:foregroundGravity="" --设置前景图位置android:layout_gravity="center" --设置居中android:background= "#999999" --设置背景android:keepScreenOn="true" --保持屏幕唤醒

4. TableLayout

TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。

如图所示:
TableLayout

TableLayout全局属性:

android:collapseColumns="n" --隐藏第 n 列android:stretchColumns= "m, n" --拉伸第 m,n 列android:shrinkColumns= "n" --压缩第 n 列

注意:索引从0开始,多个之间用逗号分隔( * 号表示所有列)

TableLayout局部属性:

android:layout_column= "n" --该控件占据第 n 列(注意:从第0列开始算起)android:layout_span= "n" --该控件占用 n 列

注:TableRow 中的控件不能指定宽度。

5. AbsoluteLayout

AbsoluteLayout是绝对布局,又可叫坐标布局,可直接指定子元素的绝对位置(xy坐标)。由于手机屏幕尺寸差别比较大,故绝对布局适应性差。因此该布局方式用的较少。了解即可。

注:部分图片来自慕课网视频截图。


发现写出一篇高质量的博文很不容易,审视自己写的,总感觉太水,或者说不算,只能算是笔记。何时才能做到写出优质博文?

0 0
原创粉丝点击