[Android]线性布局

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值