gavanwanggw

导航

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

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 the android:orientation attribute.

线性布局的全部子视图是按一个挨一个地堆叠。所以一个竖向列表每行仅仅会有一个子视图,不管多宽;水平列表仅仅有一行高(最高子视图的高度,加上内填充)。

线性布局遵守子视图间的外边距以及每个子视图的重力(靠右、居中或靠左)。
All children of a LinearLayout are 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). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.

布局权重
Layout Weight


线性布局还支持通过属性 android:layout_weight 给各个子视图赋一个权重值。

这个属性依照顾该在屏幕上占有多少空间来给一个视图赋一个权重值。更大的权重值将同意它扩展以填充父视图中剩余的空间。子视图能够指定一个权重值,这样一来全部余下的视图组空间都会分配给声明了权重的那部分子视图。默认值是 0 。
LinearLayout
 also supports assigning a weight to individual children with the android:layout_weight attribute. 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 a LinearLayout, seeLinearLayout.LayoutParams.








posted on 2017-04-14 13:37  gavanwanggw  阅读(353)  评论(0编辑  收藏  举报