android的LinearLayout布局

<?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>
<!--    android:orientation="vertical" 表明方向垂直-->
<!--  android:layout_weight="1" 表明所占权重(比例)    message占满其余元素剩余的地方,
		若是有多个android:layout_weight=1,则平分剩余空间
		 android:gravity="top"表示从上面开始占(写了android:layout_weight的)
		 
		 
		   android:layout_gravity="right"
		   整个组件的位置居右
-->

wKiom1hIFsWCFl9PAABRkFm5EbE270.png-wh_50

<?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="horizontal" >
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="wrap_content"
        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>

wKioL1hIFxSSehAQAAAj4zxp0jY739.png-wh_50

LinearLayout 是一个视图组,它全部的子视图都在一个方向对齐,水平或者垂直。你能够指定布局的方向经过 android:orientation 属性。html

LinearLayout的全部子视图排列都是一个靠着另外一个,所以垂直列表每行仅仅有一个子视图,无论有多宽。水平列表只能有一行的高度(最高子视图的高度加上边距距离)。LinearLayout 指望子视图之间都有margin,每一个子视图都有gravityjava

线性布局支持给个别的子视图设定权重,经过android:layout_weight属性。就一个视图在屏幕上占多大的空间而言,这个属性给其设定了一个重要的值。一个大的权重值,容许它扩大到填充父视图中的任何剩余空间。子视图能够指定一个权重值,而后视图组剩余的其余的空间将会分配给其声明权重的子视图。默认的权重是0.android

例如,若是有三个文本框,其中两个声明的权重为1,另一个没有权重,没有权重第三个文本字段不会增长,只会占用其内容所需的面积。其余两个一样的会扩大以填补剩余的空间,在三个文本域被测量后。若是第三个字段,而后给定的权重为2(而不是0),那么它如今的声明比其余的更重要,因此它获得一半的总的剩余空间,而前两个平均分配剩余的。ide

相关文章
相关标签/搜索