我是靠谱客的博主 懵懂红酒,最近开发中收集的这篇文章主要介绍android:布局详解LinearLayout 线性布局TableLayout 表格布局RelativeLayout 相对布局,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
属性:
蓝色
:用于布局的属性;
绿色
:用于组件的属性
LinearLayout 线性布局
线性布局不会换行,当组件排列到窗体边缘后,后面的组件将不会被显示出来,但在
weight
不为
0
的情况下,组件不会被挤出窗体。
属性:
orientation
:
线性布局的方向
grivity
:
用于布局管理器中组件内的一个属性,为组件中内容的对齐方式(如:
button
内的文字的对齐方式),默认对齐为:左上角。其可选值包括:
top
、
bottom
、
left
、
right
、
center_vertical
、
center_horizontal
、
fill_vertical
、
fill_horizontal
、
center
、
fill
、
clip_vertical
、
clip_horizontal
。这些属性可以同时指定,各属性之间用竖线隔开,例如:要指定组件靠右下角对齐,可以使用属性值:
right|bottom
Layout_weight
:
此属性情况有点复杂,它是指允许子元素可以填充屏幕上(垂直
/
水平方向)的
剩余空间
,剩余的空间就会按这些子元素指定的
weight
比例分配给这些子元素。默认的
weight
值为
0
(
意思是需要显示多大的视图就占据多大的屏幕空间)
。
有两种情况(
这里以
orientation
=
"horizontal"
为例
,
vertical
同理
):
情况
1
:
layout_width
=
"wrap_content"
时,
weight
值越大,所占比例越多:
TV1
:
layout_weight=”1”
占屏幕(垂直
/
水平方向)剩余空间的
1/4
TV2
:
layout_weight=”3”
占屏幕(垂直
/
水平方向)剩余空间的
3/4
情况
2
:
layout_width
=
" fill_parent"
时,
weight
值越小,所占比例越多,因为各元素都
fill_parent
了,所以屏幕没有了剩余空间可以分配,根据某算法,
weight
值越小,被压缩的程度越小。
算法:
假设屏幕宽度为:
pw
因为两个控件的宽度是
fill_parent
,则控件宽度为:
pw
所以:
delta
(总剩余空间)
= pw – 2 * pw = – pw
又因为:
weightSum = 1+3 =4
所以:
tv1
分配到的剩余空间
tv1_delta =
(
1 * delata
)
/weightSum
= –1/4 pw
tv1
的宽度
= pw + tv1_delta
= 3/4 pw
同理:
tv2
的宽度
= 1/4 pw
TableLayout 表格布局
Tablelayout
类以行和列的形式对控件进行管理,每一行为一个
TableRow
对象,或一个
View
控件。
1
.当为
TableRow
对象时,可在
TableRow
下添加子控件,默认情况下,每个子控件占据一列。
2
.当为
View
时,该
View
将独占一行。
TableLayout
行列数的确定
TableLayout
的行数由开发人员直接指定,即有多少个
TableRow
对象(或
View
控件),就有多少行。
TableLayout
的列数等于含有最多子控件的
TableRow
的列数。如第一
TableRow
含
2
个子控件,第二个
TableRow
含
3
个,第三个
TableRow
含
4
个,那么该
TableLayout
的列数为
4.
TableLayout
可设置的属性详解
1
、
全局属性也即列属性:
stretchColumns
:
设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。
shrinkColumns
:
设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。
collapseColumns
:
设置要隐藏的列。
示例:
android:stretchColumns="0"
第
0
列可伸展
android:shrinkColumns="1,2"
第
1,2
列皆可收缩
android:collapseColumns="*"
隐藏所有行
说明:列可以同时具备
stretchColumns
及
shrinkColumns
属性,若此,那么当该列的内容
N
多时,将
“
多行
”
显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的
layout_height
)
2
、
单元格属性:
layout_column
:指定该单元格在第几列显示
layout_span
:指定该单元格占据的列数(未指定时,为
1
)
示例:
android:layout_column="1"
该控件显示在第
1
列
android:layout_span="2"
该控件占据
2
列
说明:一个控件也可以同时具备这两个特性。
一个包含
4
个
TableLayout
布局的实例及效果图
version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="
http://schemas.android.com/apk/res/android
"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip"
>
<!--
第
1
个
TableLayout
,用于描述表中的列属性。第
0
列可伸展,第
1
列可收缩,第
2
列被隐藏
-->
<TextView
android:text="
表
1
:全局设置:列属性设置
"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
android:shrinkColumns="1"
android:collapseColumns="2"
android:padding="3dip">
<TableRow>
<Button android:text="
该列可伸展
"/>
<Button android:text="
该列可收缩
"/>
<Button android:text="
我被隐藏了
"/>
</TableRow>
<TableRow>
<TextViewandroid:text="
我向行方向伸展,我可以很长
"/>
<TextViewandroid:text="
我向列方向收缩,我可以很深
"/>
</TableRow>
</TableLayout>
<!--
第
2
个
TableLayout
,用于描述表中单元格的属性,包括:
android:layout_column
及
android:layout_span-->
<TextView
android:text="
表
2:
单元格设置:指定单元格属性设置
"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dip">
<TableRow>
<Button android:text="
第
0
列
"/>
<Button android:text="
第
1
列
"/>
<Button android:text="
第
2
列
"/>
</TableRow>
<TableRow>
<TextViewandroid:text="
我被指定在第
1
列
" android:layout_column="1"/>
</TableRow>
<TableRow>
<TextView
android:text="
我跨
1
到
2
列,不信你看!
"
android:layout_column="1"
android:layout_span="2"
/>
</TableRow>
</TableLayout>
<!--
第
3
个
TableLayout
,使用可伸展特性布局
-->
<TextView
android:text="
表
3
:应用一,非均匀布局
"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="3dip"
>
<TableRow>
<Button android:text="
一
" ></Button>
<Button android:text="
两字
"></Button>
<Button android:text="
三个字
" ></Button>
</TableRow>
</TableLayout>
<!--
第
4
个
TableLayout
,使用可伸展特性,并指定每个控件宽度一致,如
1dip-->
<TextView
android:text="
表
4
:应用二,均匀布局
"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="3dip"
>
<TableRow>
<Button android:text="
一
" android:layout_width="1dip"></Button>
<Button android:text="
两字
" android:layout_width="1dip"></Button>
<Button android:text="
三个字
" android:layout_width="1dip"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
说明:第
4
个
TableLayout
里的均匀布局的均匀效果是有限的。其有限性体现在,当该行有
N
列,则每列的控件内容不能多于
1/N
。
RelativeLayout 相对布局
属性:
Layout_above
:
在给定控件之上
Layout_below
:
在给定控件之下
Layout_toLeftOf
:
在给定控件的左边
Layout_toRightOf
:
在给定控件的右边
Layout_alignBaseline
:
与给定控件的
baseline
对齐
Layout_alignBottom
:
与给定控件的底部对齐
Layout_alignLeft :
Layout_alignRight :
Layout_alignTop :
最后
以上就是懵懂红酒为你收集整理的android:布局详解LinearLayout 线性布局TableLayout 表格布局RelativeLayout 相对布局的全部内容,希望文章能够帮你解决android:布局详解LinearLayout 线性布局TableLayout 表格布局RelativeLayout 相对布局所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复