
图 1 AndroID SDK 布局方式结构图
以下将对 FrameLayout(单帧布局)、linearLayout(线性布局)、absoluteLayout(绝对布局)、relativeLayout(相对布局)和tableLayout(表格布局)进行简单的介绍。
linearLayout(线性布局)线性布局(linearLayout)主要以水平或垂直方式来显示界面中的控件。当控件水平排列时,显示顺序依次为从左到右;当控件垂直排列时,显示顺序依次为从上到下。线性布局中,每行或每列中只允许有一个子视图或控件。linearLayout的最主要的属性有:
androID:gravity:设置内部控件的显示位置。
androID:orIEntation:设置内部控件的排列方向,常量horizontal(默认值)表示水平排列,vertical表示垂直排列。
androID:layout_weight:设置内部控件在linearLayout中所占的权重。
注意:当控件使用权重属性时,布局宽度或高度属性值通常设置为0。
所有的组件都有一个 layout_weight 值,默认为0。意思是需要显示多大的视图就占据多大的屏幕空间。
若赋值为大于 0 的值,则将可用的空间分割,分割的大小取决于当前的 layout_weight 数值与其他空间的 layout_weight 值的比率,即权重。
实例 linearLayoutDemo 演示了 linearLayout 布局的使用方法,效果如下图所示。点击Layout,右键New->Layout resource file,给这个布局起一个名字。新建了一个two.xml->点击Design->拖拽button(3个)->形成如下图:
按住Ctrl,点击图中三个button,再点击右侧菜单中layout_wIDth的下拉菜单,将match_parent改为wrap_content,形成如图:
点击Text,查看相关代码,如下:
1 <?xml version="1.0" enCoding="utf-8"?> 2 <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" 3 androID:orIEntation="vertical" 4 androID:layout_wIDth="match_parent" 5 androID:layout_height="match_parent"> 6 7 <button 8 androID:ID="@+ID/button7" 9 androID:layout_wIDth="wrap_content"10 androID:layout_height="wrap_content"11 androID:text="button" />12 13 <button14 androID:ID="@+ID/button8"15 androID:layout_wIDth="wrap_content"16 androID:layout_height="wrap_content"17 androID:text="button" />18 19 <button20 androID:ID="@+ID/button10"21 androID:layout_wIDth="wrap_content"22 androID:layout_height="wrap_content"23 androID:text="button" />24 </linearLayout>将第三行的vertical,改为horizontal,可以看到一改完这个界面就变成水平排列的了。linearLayout 布局通过 androID:orIEntation="horizontal" 属性将布局设置为横向线性排列。linearLayout 布局通过 androID:orIEntation="vertical" 属性将布局设置为纵向线性排列。
在上面的代码第三与第四行之间添加一句:androID:gravity="center",可以看到三个button就居中了(左图),而改为androID:gravity="center_horizontal",可以看到三个button就水平居中了(右图)。
将源代码改为如下:
1 <?xml version="1.0" enCoding="utf-8"?> 2 <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" 3 androID:orIEntation="vertical" 4 androID:gravity="center" 5 androID:layout_wIDth="match_parent" 6 androID:layout_height="match_parent"> 7 8 <button 9 androID:ID="@+ID/button7"10 androID:layout_weight="1"11 androID:layout_wIDth="wrap_content"12 androID:layout_height="0dp"13 androID:text="button" />14 15 <button16 androID:ID="@+ID/button8"17 androID:layout_weight="1"18 androID:layout_wIDth="wrap_content"19 androID:layout_height="0dp"20 androID:text="button" />21 22 <button23 androID:ID="@+ID/button10"24 androID:layout_weight="1"25 androID:layout_wIDth="wrap_content"26 androID:layout_height="0dp"27 androID:text="button" />28 </linearLayout>你可能会奇怪,每一个button中有两个androID:layout_weight,第一个则是每个button在其中所占的权重,而上面有三个,每个都为1,则每个的权重即:每个权重(即1)/所有权重的和(即3),也就是1/3;如上图所示!!!第二个wrap_content:是layout_wIDth和layout_height的属性值之一,表示和自身内容一样的长度。linearLayout 布局可使用嵌套。活用 linearLayou 布局可以设计出各种各样漂亮的布局方式。
总结
以上是内存溢出为你收集整理的Android五大布局:FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout和TableLayout全部内容,希望文章能够帮你解决Android五大布局:FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout和TableLayout所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)