安卓开发学习5-2:布局管理器:线性布局管理器

Source

线性布局管理器LinearLayout

LinearLayout解析

按照垂直或者水平的方式进行布局,可以通过andriod:orientation进行指定是水平布局还是垂直布局
在这里插入图片描述

无论水平方向还是垂直方向,其内部防止的组件都会按对应方向一直防止,如果超出屏幕,默认情况下是会消失,而不是产生滚动之类的显示

重要属性

1、andriod:orientation:线性布局的方向
2、android:gravity:控制子组件的排列方式
3、android:layout_gravity:控制自身在父容器的排列方式
4、android:layout_weight:用于分配剩余空间。初始值是0

如下两个组件分别是80和120,剩余空间是120,那么设置两个组件android:layout_weight都为1,那么就会平分剩余空间,组件1分配的是:1/(1+1)*120=60,也就是组件1分配到了60的剩余空间,加上原来80就是140
在这里插入图片描述
如果组件1设置android:layout_weight为1,组件2不设置(默认android:layout_weight为0),那么组件1分配到的是1/(1+0)*120,那就是拿到了所有的剩余空间

线性布局案例

1、实现简易登录窗
在这里插入图片描述
2、xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入QQ号"
        android:drawableLeft="@mipmap/account"
        ></EditText>


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入QQ号"
        android:drawableLeft="@mipmap/pwd"

        ></EditText>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:background="#FF009658"
        android:textColor="@color/white"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"></Button>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="登录遇到问题?"></TextView>
        
</LinearLayout>

3、资源
在这里插入图片描述
在这里插入图片描述