1、Android 布局详解图文 Android 布局详解 【图文】原文分四篇 在这里我将其整理方便大家学习下载: 下载地址:Android 布局是开发中非常重要的一个知识部分,它的布局分为以下几种:Linear Layout:线性布局Relative Layout:相对布局Table Layout:表格布局Grid View:网格布局Tab Layout:选项卡布局List View:列表布局一、Linear Layout简单来说,直着排,横着排都可以,还可以嵌套,此布局运用的非常多。下面直接上示例代码及截图:接下来,看一下布局XML文件:!-l version=下面详细详解这些配置的含义:Li
2、nearLayout 标签的常用属性android:orientation=horizontal:定义方向水平或垂直(horizontal/vertical)android:layout_width=fill_parent :宽度填充满父控件的宽度android:layout_height=fill_parent:宽度填充满父控件的高度android:layout_weight=1:重量?可解释为权重,这是个什么意思呢,请看下图我将这里的配置变了一下, 可以看到我设置成了1,2,3,4,这四TextView显示的宽度不一样了,具体是怎么算的,这个我们就不追究了,意思清楚就行,都设置为1则平分,
3、否则数给的越大,占的位置就越多。再看一下TextView的解释android:text=red:要显示的内容android:gravity=center_horizontal:显示内容的对齐方式android:background=#aa0000 :背景色android:layout_width=wrap_content:宽度,包括自己的内容的宽度android:layout_height=fill_parent:高度,填充父控件的高度android:layout_weight=1:权重其实含义如果懂些CSS属性的话,还是蛮好懂的,布局跟Div有点类似/类似一个外层DIV,里面的内容垂直布局a
4、ndroid:orientation=vertical /类似第一个子DIV,内容水平布局android:orientation=horizontal /类似第二个子DIV,内容垂直布局android:orientation=vertical 先把大的框框画好,再在各个子框里面细画内容,这个布局蛮简单的。二、Relative LayoutRelative LayoutRelative Layout布局:相对位置布局,类似于Word中的位置对齐,总共有九个方位,如下图所示下面,我们来看看布局文件的代码与详解:view plain1. 2. 3. 6. 11. 17. 25. 31. Relati
5、veLayout 声明此Activity使用相对布局,android:layout_width=fill_parent:宽度填充父控件的宽度android:layout_height=fill_parent:高度填充父控件的高度TextView /一个标签android:id=+id/label /组件IDandroid:layout_width=fill_parent /宽,填充父控件android:layout_height=wrap_content /高,刚刚包含自己的内容的高度,相当于自适应android:text=Type here: /标签显示的内容EditText /一个文本框a
6、ndroid:id=+id/entry /组件IDandroid:layout_width=fill_parent /宽android:layout_height=wrap_content /高android:background=android:drawable/editbox_background /背景色android:layout_below=id/label /below英文单词译为在下,这里引用前面的标签组件,意为摆在这个标签的下面Button /一个按钮android:id=+id/ok /组件IDandroid:layout_width=wrap_content /宽度自适应a
7、ndroid:layout_height=wrap_content /高度自适应android:layout_below=id/entry /在前面组件文本框的下面android:layout_alignParentRight=true /相对父控件右对齐,这里的父控件应该是屏幕,即屏幕的右边android:layout_marginLeft=10dip/组件的外边距10个像素,margin,padding可以看看CSS的讲解.android:text=OK/按钮上显示的内容Buttonandroid:layout_width=wrap_content/宽自适应android:layout_h
8、eight=wrap_content/高自适应android:layout_toLeftOf=id/ok /使当前控件置于id为ok的控件的左边。android:layout_alignTop=id/ok /使当前控件与id为ok控件的上端排齐。android:text=Cancel/按钮上显示的内容可以看到相对位置的对齐方式有以下图中所示的这么多:可以分别对应Word中的九个方位,以及还可以设置类似CSS中的组件与组件之间的外边距,如margin即是设置这一项,注意layout_margin这一项设置像素以后,则是设置的上下左右四个方位的值,如果只想设置某一边的话,则使用下面的marginLeft之一,设置组件内文字与组件边距可用属性padding,如下图:最后效果图为三、Table Layoutview plain1. 2. 6. 7. 8. 12. 16. 17. 18. 19. 23. 27. 28. 29. 30. 34. 38. 39. 40. 43. 44. 45. 48. Te
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2