线性布局管理器 (LinearLayout)里嵌套相对布局管理器(RelativeLayout)
线性布局管理器分为水平布局和垂直布局
水平布局(horizontal):组件从左往右进行排列,所有组件占一行,每一个组件占一列
垂直布局(vertical):组件从上到下进行排列,左右组件占一列,每一个组件占一行
相对布局管理器是按照组件的相对位置进行摆放的,我们可以指定一个组价相对于另一个组件的位置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mypc.android.MainActivity"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#FF0077FF"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="账号/手机号/邮箱/QQ号"
android:paddingTop="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="number"
android:paddingTop="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginLeft="70dp"
android:textSize="20sp"
android:textColor="#FF0077FF"
android:backgroundTint="#FF8247"/>
<Button
android:layout_toRightOf="@id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:textSize="20sp"
android:textColor="#FF0077FF"
android:backgroundTint="#FF8247"/>
</RelativeLayout>>
</LinearLayout>
效果图:
我们需要注意的就是怎么通过布局管理器的特点以及嵌套来控制各组件的位置以及布局管理器嵌套的代码格式
控制注册按钮与上面编辑框的距离代码android:layout_marginTop="30dp"
控制注册按钮与容器左边的距离代码android:layout_marginLeft="70dp"
控制登录按钮相对于注册按钮的距离代码android:layout_marginLeft="50dp"