android线性布局参数详解.doc_第1页
android线性布局参数详解.doc_第2页
android线性布局参数详解.doc_第3页
android线性布局参数详解.doc_第4页
android线性布局参数详解.doc_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

android线性布局参数详解 /trampou/article/details/5752533线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是本元素所有子元素的重力方向) android:orientation (线性布局以列或行来显示内部子元素) android:layout_weight (线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大。 前提是子元素 设置了 android:layout_width = fill_parent 属性(水平方向) 或 android:layout_height = fill_parent 属性(垂直方向) 如果某个子元素的 android:layout_width = wrap_content 或 android:layout_height = wrap_content” , 则 android:layout_weight 的设置值 对该方向上空间的分配刚好相反。 下面以一个简单例子来说明这 4个参数 说明:在上面的例子中,根布局是LinearLayout, 其包含有2 个TextView 视图,为了对参数 android:layout_gravity 有直观的了解,对根布局 LinearLayout 特意加了 3 个参数 android:layout_height = 200dp android:layout_width = 200dp android:background = #AABBCC 为布局指定了固定的宽度和高度,以及背景颜色,上面的例子运行后效果如下图: 说明:对LinearLayout 中的参数android:layout_gravity 来说,其意义是指定本布局相对于父布局的重力方向,由于该布局的已经是根布局,其父布局是整个屏幕,那么该参数设置的是相对于屏幕的位置,可以换不同的参数 top|bottom|left|right 等等参数来试验。 现在增加参数 android:gravity = bottom|right 完整 XML 如下,看看效果 通过改变android:gravity 参数的值可以看到实际效果。 参数 android:orientation= horizontal 决定了每个子元素各占一列,如果参数 android:orientation= vertical , 则每个子元素各占一行,也就是从上到下排列了。对于 LinearLayout 布局的子元素,给每个子元素加上参数 android:layout_weight 看看效果 Text 为ONE 的权重为1 ,但明显占的宽度比TWO 的小,百思不得其解,后来得知,如果把TextView 的参数android:layout_width = wrap_content 全部修改为 android:layout_width = fill_parent , 则 ok ,代码如下 android布局之LinearLayout Java代码 1. main.xml 2. 3. 4. 5. !- 6. 7. 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的 8. - 9. 14. 35. 40. 47. 48. 55. 56. 63. 64. 71. 72. 73. 74. 79. 80. 86. 87. 93. 94. 100. 101. 107. 108. 109. 110. main.xml!- 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的 - 感觉这种形式有点像div+css的方式布局,不过这种方式的灵活性和div+css还是有些不及,主要是那android:layout_weight的值如何去确定,而且采用的是数值越小,重要度越高的方式,分配起来还得好好计算一下。Java代码 1. Views.java 2. 3. 4. .view; 5. 6. importandroid.app.Activity; 7. importandroid.os.Bundle; 8. 9. publicclassViewsextendsActivity 10. /*Calledwhentheactivityisfirstcreated.*/11. Override12. publicvoidonCreate(BundlesavedInstanceState) 13. super.onCreate(savedInstanceState); 14. setContentView(R.layout.main); 15. 16. Views.javapackage .view;import android.app.Activity;import android.os.Bundle;public class Views extends Activity /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); 我使用的版本为1.5.欢迎朋友们讨论android:layout_gravity 和 android:gravity 的区别从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。比如说button: android:layout_gravity 表示按钮在界面上的位置。 android:gravity表示button上的字在button上的位置。可选值这两个属性可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。而且这些属性是可以多选的,用“|”分开。默认这个的值是:Gravity.LEFT对这些属性的描述:出自:/guide/topics/resources/drawable-resource.html/reference/android/graphics/drawable/ClipDrawable.htmlValue Description top Put the object at the top of its container, not changing its size. 将对象放在其容器的顶部,不改变其大小. bottom Put the object at the bottom of its container, not changing its size. 将对象放在其容器的底部,不改变其大小. left Put the object at the left edge of its container, not changing its size. 将对象放在其容器的左侧,不改变其大小. right Put the object at the right edge of its container, not changing its size. 将对象放在其容器的右侧,不改变其大小. center_vertical Place object in the vertical center of its container, not changing its size. 将对象纵向居中,不改变其大小. 垂直对齐方式:垂直方向上居中对齐。 fill_vertical Grow the vertical size of the object if needed so it completely fills its container. 必要的时候增加对象的纵向大小,以完全充满其容器. 垂直方向填充 center_horizontal Place object in the horizontal center of its container, not changing its size. 将对象横向居中,不改变其大小. 水平对齐方式:水平方向上居中对齐 fill_horizontal Grow the horizontal size of the object if needed so it completely fills its container. 必要的时候增加对象的横向大小,以完全充满其容器. 水平方向填充 center Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. 将对象横纵居中,不改变其大小. fill Grow the horizontal and vertical size of the object if needed so it completely fills its container. This is the default. 必要的时候增加对象的横纵向大小,以完全充满其容器. clip_vertical Additional option that can be set to have the top and/or bottom edges of the child clipped to its containers bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges. 附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.垂直方向裁剪 clip_horizontal Additional option that can be set to have the left and/or right edges of the child clipped to its containers bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges. 附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.水平方向裁剪 简单记忆 : horizontal 都是操作的水平方向,即横向, vertical 都是炒作的垂直方向,即纵向。对于LinearLayout何时生效的问题参看:也谈layout_gravity和gravity /viewthread.php?tid=325对于 LinearLayout当 android:orientation=vertical 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。当 android:orientation=horizontal 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。Android教程TextView文本垂直/水平居中(android:gravity/layout_gravity)Android教程TextView文本垂直/水平居中(android:gravity/layout_gravity)作者:lenomon 发布:2012-04-28 21:38 TextView要让文本垂直/水平居中显示,有两种情况需要考虑:1、layout_width/layout_height为wrap_content,此时要让TextView在父控件上居中显示,必须设置layout_gravity=”center”。2、layout_width/layout_height为fill_parent,此时由于TextView已占据父窗体所有空间,必须设置gravity=”center”。PS:android:gravity用来设置TextView的内容对齐方式,android:layout_gravity用来设置TextView在父窗体中的对齐方式。Java代码设置如下:12345678910mTVText.setGravity(Gravity.CENTER); /左上角设置为:Gravity.LEFT|Gravity.TOP /LinearLayout居中LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);params.gravity = Gravity.CENTER_HORIZONTAL;/设置TextView在LinearLayout上的外边距(此处为距父窗体四周5个像素),建议单位使用dp,关于单位转换在下面说明params.setMargins(5, 5, 5, 5);/RelativeLayout居中RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);params2.addRule(RelativeLayout.CENTER_VERTICAL);PS:上面说了Line

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论