AndroidStdio RelativeLayout相对布局写一个注册界面
先看效果图:
注册界面包括两部分,上面的图片区和下面的表单区。图片区包含一个ImageView,表单区包含昵称文本框,昵称输入框,密码文本框,密码输入框,确认密码文本框,确认密码输入框和一个注册按钮。完整代码放在最下面。下面简单说一下RelativeLayout相对布局。
概览
相对布局是组件相对于某个参照组件进行布局的方式,它比线性布局更加灵活。可以是父组件和子组件之间,也可以是子组件和子组件之间。
父子之间的相对布局
常用的有如下一些:
示例效果:
兄弟之间的相对布局
常用的有如下一些:
示例效果:
注册界面代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".RegisterActivity">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="304dp"
app:srcCompat="@drawable/register" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView3"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="昵称"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView5"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="密码" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView6"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="确认密码" />
<EditText
android:id="@+id/password2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView7"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/buttonRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:background="@color/colorRegister"
android:text="注册" />
</RelativeLayout>
out_marginBottom="40dp"
android:background="@color/colorRegister"
android:text="注册" />
</RelativeLayout>