Android 线性布局(LinearLayout)相关官方文档 - 指南部分

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 - 本博客专注于敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。



Android 官方文档线性布局相关资源链接汇总如下:

android-sdk-macosx-4.4.2/docs/guide/topics/ui/layout/linear.html

android-sdk-macosx-4.4.2/docs/reference/android/widget/LinearLayout.html

android-sdk-macosx-4.4.2/docs/reference/android/widget/LinearLayout.LayoutParams.html



由于在线文档访问困难,这里给出的是本地文档的相对路径。



重点摘要:

1、未指定权重的子视图,其是否占空间,所占空间具体由什么来决定,是按内容,还是按设定的宽、高值,或者由子视图重载 android.view.View的onMeasure 方法?

a、理论分析结果:由原文句子 “The other two will expand equally to fill the space remaining after all three fields are measured.”,可以初步推断是按onMeasure测定的宽、高来确定所占空间。而针对于大多数组件,会重载该方法来确定其内容所占空间,也即这一组件的宽、高设定为 "wrap_content" 所占空间。

b、实际测试结果待补充。

2、为何正文中提示,宽、高属性要设置为 "0dp" ?如果没设置为"0dp" ,其值是否会对子视图本身所占的无权重空间有影响?如果按权重分配的空间没有设定的宽或高值大,会怎样?如果宽或高设置为"0dp" ,此时权重分配的空间仍没有其本身内容占用空间大,会怎样?

a、理论分析结果:没思路,不知道会是啥结果;

b、实际测试结果待补充。


译文来源:guide/topics/ui/layout/linear.html

线性布局 Linear Layout

本文档中内容
IN THIS DOCUMENT

  1. 布局权重
    Layout Weight
  2. 示例
    Example

关键类
KEY CLASSES

  1. 线性布局
    LinearLayout
  2. 线性布局.布局参数
    LinearLayout.LayoutParams

线性布局 是一个视图分组,它在单一方向上对齐所有的子视图,或者竖直方向或者水平方向。可以使用 android:orientation属性来指定布局方向。
LinearLayout
is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with theandroid:orientationattribute.

线性布局的所有子视图是按一个挨一个地堆叠,所以一个竖向列表每行只会有一个子视图,无论多宽;水平列表只有一行高(最高子视图的高度,加上内填充)。线性布局遵守子视图间的外边距以及每一个子视图的重力(靠右、居中或靠左)。
All children of aLinearLayoutare stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). ALinearLayoutrespectsmargins between children and thegravity(right, center, or left alignment) of each child.

布局权重
Layout Weight

平等加权子视图
Equally weighted children

要创建一个线布局,要在其内部,使每一个子视图使用相同数量的屏幕空间,那么设置每一个设图的android:layout_height属性为 "0dp"(针对于竖向布局)或者android:layout_width属性为 "0dp"(针对于水平布局),然后,为每一个视图设置android:layout_weight属性值为 "1"。
To create a linear layout in which each child uses the same amount of space on the screen, set theandroid:layout_heightof each view to"0dp"(for a vertical layout) or theandroid:layout_widthof each view to"0dp"(for a horizontal layout). Then set theandroid:layout_weightof each view to"1".

线性布局还支持通过属性android:layout_weight给各个子视图赋一个权重值。这个属性按照应该在屏幕上占有多少空间来给一个视图赋一个权重值。更大的权重值将允许它扩展以填充父视图中剩余的空间。子视图可以指定一个权重值,这样一来所有余下的视图组空间都会分配给声明了权重的那部分子视图。默认值是 0 。
LinearLayout
also supports assigning aweightto individual children with theandroid:layout_weightattribute. This attribute assigns an "importance" value to a view in terms of how much space is should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

例如,如果有三个文本哉,并且其中两个声明了 1 的权重,而另一个没有给定权重,那么第三个没有权重的文本域不会增长,只会占用其内容必需的区域(待测试:如果未指定权重而指定了宽或高,那么内容必需的区域是指实际内容还是宽、高值?还是通过 onMeasured 方法获得该视图的内容必需的区域?)。其它两个域就会平等地填充在所有三个域测量后余下的空间。如果第三个字段随后给定了一个 2 的权重(而不是 0 了),那么它现在声明的比其它两个权重更重了,这样它获得总共余下空间的一半,而头两个等分余下的部分。
For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

例子
Example

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:paddingLeft="16dp"  android:paddingRight="16dp"  android:orientation="vertical" >  <EditText    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:hint="@string/to" />  <EditText    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:hint="@string/subject" />  <EditText    android:layout_width="fill_parent"    android:layout_height="0dp"    android:layout_weight="1"    android:gravity="top"    android:hint="@string/message" />  <Button    android:layout_width="100dp"    android:layout_height="wrap_content"    android:layout_gravity="right"    android:text="@string/send" /></LinearLayout>

有关线性布局每一个子视图可用的属性的详细描述,可以查看LinearLayout.LayoutParams。
For details about the attributes available to each child view of aLinearLayout, seeLinearLayout.LayoutParams.

Except as noted, this content is licensed under Creative Commons Attribution 2.5. For details and restrictions, see the Content License.

About Android|Legal|Support








更多相关文章

  1. android中自定义ViewGroup的实现
  2. Android(安卓)线性布局(LinearLayout)性能相关
  3. 记录-解决设置透明状态栏,软键盘弹起问题
  4. android 开发的一个小警告
  5. Android(安卓)Notes 之 Tween动画 (3)添加布局动画
  6. Android(安卓)fragment
  7. Android(安卓)base-adapter-helper 源码分析与扩展
  8. android:weight属性的使用——android开发之xml布局文件
  9. Android横向滚动屏幕特效分析

随机推荐

  1. Tip of the Day(Android Studio)
  2. Android笔记(28)MVVM架构过程
  3. 下载不同版本android studio
  4. 2013.07.08——— android MediaRecorder
  5. Activity com.avcit.conference.MainActi
  6. android应用不生成桌面应用图标
  7. Android 自定义progressDialog
  8. Wifi广播状态
  9. Android Studio 依赖添加
  10. Android 解码MediaCodec 播放H264 265