【Android】UI布局之线性布局(登录界面代码)

1、布局管理

组件在activity中呈现的方式,包含组件大小、间距、对齐方式

Android提供了两种布局的实现方式:

  • .在xml配置文件中声明,通过setContentView(R.layout.main)方法呈现在activity中,通过findViewById()方法获得组件实例。(一般推荐这种方式)
  • 动态生成组件以及设置相关布局

2、线性布局 LinearLayout

线性布局是最简单的一种布局,将子组件按照垂直或者水平方向进行布局。

  • 方向控制:android:orientation属性来控制,有vertical(垂直)和horizontal(水平)两种
  • 对齐方式:android:gravity属性来控制,有top、bottom、left、right、center等
  • 比例分割:weight

3、登录界面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="13dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="username:"
            android:layout_marginLeft="15dp"/>


    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="13dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="password:"
            android:layout_marginLeft="15dp"/>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="45dp"
            android:layout_marginRight="10dp"
            android:text="submit" />

        <Button
            android:id="@+id/button2"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:text="reset" />
    </LinearLayout>
</LinearLayout>

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值