您的位置:首页 > 产品设计 > UI/UE

Android UI组件----用相对布局RelativeLayout做一个登陆界面

2014-07-12 14:54 573 查看
【声明】

欢迎转载,但请保留文章原始出处→_→

生命壹号:http://www.cnblogs.com/smyhvae/

文章来源:/article/4579374.html

【正文】

两个小时的学习成果,对于我这种还没入门但渴望不断进步的初学者来说,是一种激励。通过自己一行一行的敲代码,实现了用相对布局做一个登陆界面的效果。

XML文件完整版代码如下:

<RelativeLayout 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"
android:padding="20dp" >

<!--定义"登陆界面"四个字的文本框,放在最上方的中央  -->
<TextView
android:id="@+id/lableView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="登陆界面" />

<!--定义输入用户名的文本编辑框  -->
<EditText
android:id="@+id/usernameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lableView"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="username"    />

<!--定义输入密码的文本编辑框  -->
<EditText
android:id="@+id/passwordView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/usernameView"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:hint="password"
android:inputType="textPassword" />

<!--定义“取消”的按钮  -->
<Button
android:id="@+id/cancleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/passwordView"
android:layout_alignParentRight="true"
android:text="取消" />

<!--定义“确定”的按钮,放在“取消”按钮的左边  -->
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/cancleButton"
android:layout_toLeftOf="@id/cancleButton"
android:text="确定" />

</RelativeLayout>


代码解释如下:

05行:设置整个RelativeLayout的内边距为20dp,给界面留白。
21、22行:让编辑框的左边对齐到父控件的左边,右边对齐到父控件的右边,这样一来,就刚好横向覆盖整个界面。
30行:将passwordView放到usernameView的下面,以保证二者的相对位置不变。
34行:输入密码时显示为星号,以保证密码的安全

41、42行:将“取消”按钮放到passwordView的下面,并贴齐到父控件的右边
50、51行:将“确定”按钮放到“取消”按钮的左边,并贴齐到“取消”按钮的下边,以保证二者的相对位置不变
最终在手机上运行后,显示效果如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: