中区网站建设,网站建设 环讯传媒,网站开发有哪些公司,宁波龙山建设有限公司网站layout #xff08;布局#xff09; ---Android 有五大布局#xff0c;分别是#xff1a; LinearLayout : 线性布局#xff0c;子组件按照垂直或者水平方向来布局。RelativeLayout #xff1a;相对布局#xff0c;按照控件之间的相互位置进行参照物的概念排布…layout 布局 ---Android 有五大布局分别是 LinearLayout : 线性布局子组件按照垂直或者水平方向来布局。RelativeLayout 相对布局按照控件之间的相互位置进行参照物的概念排布存在一个排布存在一个参照物的概念, 一般来说在相对布局中的控件都会存在id 的属性。TableLayout 表格布局继承于线性布局。用法类似AbsoluteLayout 绝对布局是通过指定控件的x/y坐标来定位的显示效果死板不利于开发几乎被弃用。FrameLayout 帧布局画面显示的效果一、线性布局LiearLayout 线性布局在开发中使用最多具有垂直方向与水平方向的布局方式 通过设置属性“android:orientation”控制方向属性值垂直vertical和水平(horizontal)默认水平方向。 常用属性 android:layout_gravity 本元素相对于父元素的重力方向 android:gravity 本元素所有子元素的重力方向 android:orientation 线性布局以列或行来显示内部子元素 android:layout_weight 子元素对未占用空间水平或垂直分配权重值 当 android:orientationvertical 时 只有水平方向的设置才起作用垂直方向的设置不起作用。即leftrightcenter_horizontal 是生效的。 当 android:orientationhorizontal 时 只有垂直方向的设置才起作用水平方向的设置不起作用。即topbottomcenter_vertical 是生效的。 android:layout_gravity 和 android:gravity 的区别 android:gravity 对元素本身起作用-本身元素显示在什么位置 android:layout_gravity 相对与它的父元素-元素显示在父元素的什么位置。 如Button控件 android:layout_gravity 表示button在界面上的位置 android:gravity表示button上的字在button上的位置。 可选值[多选时用“|”分开] top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。 top 将对象放在其容器的顶部不改变其大小.bottom 将对象放在其容器的底部不改变其大小.left将对象放在其容器的左侧不改变其大小.right将对象放在其容器的右侧不改变其大小.center_vertical 将对象纵向居中不改变其大小. 垂直对齐方式垂直方向上居中对齐。 fill_vertical 必要的时候增加对象的纵向大小以完全充满其容器. 垂直方向填充 center_horizontal 将对象横向居中不改变其大小水平对齐方式水平方向上居中对齐 fill_horizontal 必要的时候增加对象的横向大小以完全充满其容器. 水平方向填充 center 将对象横纵居中不改变其大小. fill 必要的时候增加对象的横纵向大小以完全充满其容器. clip_vertical 附加选项用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置顶部对齐时剪切底部底部对齐时剪切顶部除此之外剪切顶部和底部.垂直方向裁剪 clip_horizontal 附加选项用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置左侧对齐时剪切右侧右侧对齐时剪切左侧除此之外剪切左 侧和右侧.水平方向裁剪 例子 TextView要让文本垂直/水平居中显示有两种情况需要考虑 1、layout_width/layout_height为wrap_content此时要让TextView在父控件上居中显示必须设置layout_gravity”center”。 2、layout_width/layout_height为fill_parent此时由于TextView已占据父窗体所有空间必须设置gravity”center”。 二、相对布局 相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。参照控件可以是父控件也可以是其它子控件但是被参照的控件必须要在参照它的控件之前定义 常用属性 RelativeLayout用到的一些重要的属性 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴紧父元素的右边缘 android:layout_alignParentTop 贴紧父元素的上边缘 android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物 第二类属性值必须为id的引用名“id/id-name” android:layout_below 在某元素的下方 android:layout_above 在某元素的的上方 android:layout_toLeftOf 在某元素的左边 android:layout_toRightOf 在某元素的右边 android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐 android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐 android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐 第三类属性值为具体的像素值如30dip40px android:layout_marginBottom 离某元素底边缘的距离 android:layout_marginLeft 离某元素左边缘的距离 android:layout_marginRight 离某元素右边缘的距离 android:layout_marginTop 离某元素上边缘的距离 EditText的android:hint 设置EditText为空时输入框内的提示信息。 android:gravity android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左靠右等位置.以button为例android:gravityright则 button上面的文字靠右 android:layout_gravity android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里你想把该button放在靠左、靠右等位置就可以通过该属性设置. 以button为例android:layout_gravityright则button靠右 android:layout_alignParentRight 使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false默认false。 android:scaleType android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别 CENTER /center 按图片的原来size居中显示当图片长/宽超过View的长/宽则截取图片的居中部分显示 CENTER_CROP / centerCrop 按比例扩大图片的size居中显示使得图片长(宽)等于或大于View的长(宽) CENTER_INSIDE / centerInside 将图片的内容完整居中显示通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽 FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度居中显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度显示在View的下部分位置 FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度显示在View的上部分位置 FIT_XY / fitXY 把图片 不按比例扩大/缩小到View的大小显示 MATRIX / matrix 用矩阵来绘制动态缩小放大图片来显示。 ** 要注意一点Drawable文件夹里面的图片命名是不能大写的。 例子 1 ?xml version1.0 encodingutf-8?2 ?xml version1.0 encodingutf-8?3 RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android4 android:layout_widthfill_parent5 android:layout_heightfill_parent6 7 AnalogClock8 android:idid/aclock9 android:layout_widthwrap_content
10 android:layout_heightwrap_content
11 android:layout_centerInParenttrue /
12 DigitalClock
13 android:idid/dclock
14 android:layout_widthwrap_content
15 android:layout_heightwrap_content
16 android:layout_belowid/aclock
17 android:layout_alignLeftid/aclock
18 android:layout_marginLeft40px /
19 TextView
20 android:layout_widthwrap_content
21 android:layout_heightwrap_content
22 android:text当前时间
23 android:layout_toLeftOfid/dclock
24 android:layout_alignTopid/aclock/
25 /RelativeLayout 三、帧布局FrameLayout 帧布局又叫框架布局是五大布局中最简单的一个布局在这个布局中整个界面被当成一块空白备用区域所有的子元素都不能被指定放置的位置它们统统放于这块区域的左上角并且后面的子元素直接覆盖在前面的子元素之上将前面的子元素部分和全部遮挡。显示效果如下第一个TextView被第二个TextView完全遮挡第三个TextView遮挡了第二个TextView的部分位置 常用属性 android:foreground:设置改帧布局容器的前景图像 android:foregroundGravity:设置前景图像显示的位置 例子 1 ?xml version1.0 encodingutf-8?2 FrameLayout xmlns:androidhttp://schemas.android.com/apk/res/android3 android:layout_widthfill_parent android:layout_heightfill_parent4 LinearLayout android:idid/linearLayout15 android:layout_heightmatch_parent6 android:layout_widthmatch_parent7 Button android:textButton8 android:idid/button19 android:layout_widthwrap_content
10 android:layout_heightwrap_content/Button
11 /LinearLayout
12 LinearLayout android:layout_widthmatch_parent
13 android:idid/linearLayout3
14 android:layout_heightmatch_parent
15 android:gravitybottom|right
16 Button android:textButton
17 android:idid/button3
18 android:layout_widthwrap_content
19 android:layout_heightwrap_content/Button
20 /LinearLayout
21 LinearLayout android:layout_heightmatch_parent
22 android:idid/linearLayout2
23 android:layout_widthmatch_parent
24 android:gravityright
25 Button android:textButton
26 android:idid/button2
27 android:layout_widthwrap_content
28 android:layout_heightwrap_content/Button
29 /LinearLayout
30 LinearLayout android:layout_widthmatch_parent
31 android:idid/LinearLayout01
32 android:layout_heightmatch_parent
33 android:gravitybottom|left
34 Button android:idid/Button01
35 android:textButton
36 android:layout_widthwrap_content
37 android:layout_heightwrap_content/Button
38 /LinearLayout
39 /FrameLayout 四、绝对布局AbsoluteLayout 绝对布局的子控件需要指定相对于此坐标布局的横纵坐标值否则将会像框架布局那样被排在左上角。手机应用需要适应不同的屏幕大小而这种布局模型不能自适应屏幕尺寸大小所以应用的相对较少。 常用属性 1.控制大小 android:layout_width组件高度 android:layout_height:组件高度 2.控制位置 android:layout_x:设置组件的X坐标 android:layout_y:设置组件的Y坐标 由于该布局应用低不同的手机屏幕大小不同显示的布局也不一样所以我就部深入了解了 五、表格布局TableLayout 表格布局模型以行列的形式管理子控件每一行为一个TableRow的对象当然也可以是一个View的对象。TableRow可以添加子控件每添加一个为一列。 常用属性 android:collapseColumns:将TableLayout里面指定的列隐藏若有多列需要隐藏请用逗号将需要隐藏的列序号隔开。 android:stretchColumns:设置指定的列为可伸展的列以填满剩下的多余空白空间若有多列需要设置为可伸展请用逗号将需要伸展的列序号隔开。 android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时将列序号用逗号隔开。 列元素(Button)属性奇怪的是button 里面没有android:layout_column 和android:layout_span两个属性写进去无反应还不知道为什么 android:layout_colum:设置该控件在TableRow中指定的列。 android:layout_span:设置该控件所跨越的列数。 例子 1 ?xml version1.0 encodingutf-8?2 TableLayout xmlns:androidhttp://schemas.android.com/apk/res/android3 android:layout_widthfill_parent4 android:layout_heightfill_parent5 android:stretchColumns16 TableRow7 TextView8 android:layout_column19 android:padding3dip android:textRow1/
10 TextView
11 android:text1
12 android:gravityright
13 android:padding3dip /
14 /TableRow
15 View
16 android:layout_height2dip
17 android:background#FF909090 /
18 TableRow
19 TextView
20 android:text*
21 android:padding3dip /
22 TextView
23 android:textRow12
24 android:padding3dip /
25 TextView
26 android:text2
27 android:gravityright
28 android:padding3dip /
29 /TableRow
30 View
31 android:layout_height2dip
32 android:background#FF909090 /
33 TableRow
34 TextView
35 android:layout_column1
36 android:textRow13
37 android:padding3dip /
38 /TableRow
39 /TableLayout 转载于:https://www.cnblogs.com/hxb2016/p/6090286.html