[Android]线性布局

来源:互联网 发布:基金数据库 编辑:程序博客网 时间:2024/06/04 21:15

LinearLayout即:线性布局,是一个视图组。用于对齐所有的其包含的子视图。所有的子视图在一个方向,垂直或水平。可以通过android:orientation 属性来指定布局方向。

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 padd ing). A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.
所有子视图被挨个堆放,所有如果是垂直方向,每一行将只有一个子视图;如果是水平方向,将只会有一行。线性布局指示了子视图之间的权重,比如靠右对齐,居中对齐,左对齐等。

线性布局还有一个很重要的属性:android:layout_weight。它表示子视图在整个线性布局中所占的权重,权重越大,所占有的屏幕比例越大。

下面通过一个简单的登录功能,展示线性布局的使用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:paddingLeft="10dp"        android:paddingRight="10dp"        android:paddingTop="100dp" >        <TextView            android:layout_width="80dp"            android:layout_height="50dp"            android:text="@string/username"            android:textSize="20sp" />        <EditText            android:layout_width="match_parent"            android:layout_height="50dp"            android:hint="@string/placeholder" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:paddingLeft="10dp"        android:paddingTop="30dp" >        <TextView            android:layout_width="80dp"            android:layout_height="50dp"            android:text="@string/password"            android:textSize="20sp" />        <EditText            android:layout_width="match_parent"            android:layout_height="50dp"            android:hint="@string/placeholder2" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal"        android:paddingLeft="10dp"        android:paddingRight="10dp"        android:paddingTop="100dp" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="@string/login" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:hint="@string/reset" />    </LinearLayout></LinearLayout>
0 0
原创粉丝点击