本文主要介绍 Android 登录界面实现,这里主要讲解类似 Twitter 的登录界面的实现,有兴趣的小伙伴可以参考下
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
本文要演示的 Android 开发实例是如何完成一个 Android 中的 miniTwitter 登录界面,下面将分步骤讲解怎样实现图中的界面效果,让大家都能轻松的做出美观的登录界面。
miniTwitter 登录界面效果图
先贴上最终要完成的效果图:
miniTwitter 登录界面的布局分析
首先由界面图分析布局,基本可以分为三个部分,下面分别讲解每个部分。
第一部分是一个带渐变色背景的 LinearLayout 布局,关于背景渐变色就不再贴代码了,效果如下图所示:
第二部分,红色线区域内,包括 1,2,3,4 如图所示:
红色的 1 表示的是一个带圆角且背景色为 #55FFFFFF(淡蓝色)的 RelativeLayout 布局,代码如下:
XML/html 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#55FFFFFF" />
- <!-- 设置圆角
- 注意: bottomRightRadius是左下角而不是右下角 bottomLeftRadius右下角-->
- <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"
- android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>
- </shape>
solid 表示填充色,这里填充的是淡蓝色。corners 是设置圆角。
dp (即 dip,device independent pixels) 设备独立像素:这个和设备硬件有关,一般我们为了支持 WVGA、HVGA 和 QVGA ,不依赖像素。在 android 上开发的程序将会在不同分辨率的手机上运行。为了让程序外观不至于相差太大,所以引入了 dip 的概念。比如定义一个矩形 10 x 10dip. 在分辨率为 160dpi 的屏上,比如 G1,正好是 10 x 10 像素。 而在 240 dpi 的屏,则是 15 x 15 像素。换算公式为 pixs = dips * (density/160)。density 就是屏的分辨率。
然后 RelativeLayou 的 background 引用此 drawable,具体 RelativeLayout 设置如下:
XML/HTML 代码
- <RelativeLayout
- android:id="@+id/login_div"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="15dip"
- android:layout_margin="15dip"
- android:background="@drawable/background_login_div_bg"
- >
- </RelativeLayout>
padding 是指内边距 (也就是指内容与边框的距离),layout_margin 为外边距 (它的上一层与边框的距离)。
接下来是区域 2,为账号的文本和输入框,首先是账号的文本,代码如下:
XML/HTML 代码
- <TextView
- android:id="@+id/login_user_input"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_marginTop="5dp"
- android:text="@string/login_label_username"
- style="@style/normalText"/>
android:layout_alignParentTop 这里表示此 TextView 的位置处于顶部
android:layout_marginTop="5dp" 这里表示此 TextView 的边框与 RelativeLayout 的顶部边框距离有 5dp
这里需要对这个 TextView 设置下字体颜色和字体大小,定义在 res/style.xml 里面:
XML/HTML 代码
- <style name="normalText" parent="@android:style/TextAppearance">
- <item name="android:textColor">
- #444
- </item>
- <item name="android:textSize">
- 14sp
- </item>
- </style>
定义账号的输入框,如下:
XML/HTML 代码
- <EditText
- android:id="@+id/username_edit"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="@string/login_username_hint"
- android:layout_below="@id/login_user_input"
- android:singleLine="true"
- android:inputType="text"/>
android:hint 输入框里面的提示文字,android:layout_below 这里是设置为在账号的文本框的下面,android:singleLine 为单行输入(即你输入回车的时候不会在换行了),android:inputType 这里 text 表示输入的类型为文本。
区域 3 是密码文本和输入框,同区域 2,代码如下:
XML/HTML 代码
- <TextView
- android:id="@+id/login_password_input"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/username_edit"
- android:layout_marginTop="3dp"
- android:text="@string/login_label_password"
- style="@style/normalText"/>
- <EditText
- android:id="@+id/password_edit"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/login_password_input"
- android:password="true"
- android:singleLine="true"
- android:inputType="textPassword"
- />
区域 4,登录按钮:
XML/HTML 代码
- <Button
- android:id="@+id/signin_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/password_edit"
- android:layout_alignRight="@id/password_edit"
- android:text="@string/login_label_signin"
- android:background="@drawable/blue_button"
- />
第三部分:底下的文字和两张图片,分别标记了 1,2,3,4:
区域 1:还是一个 RelativeLayout,但这里设置的很简单,代码如下:
XML/HTML 代码
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </RelativeLayout>
XML/HTML 代码
定义如下:
XML/HTML 代码
- <TextView android:id="@+id/register_link"
- android:text="@string/login_register_link"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="15dp"
- android:textColor="#888"
- android:textColorLink="#FF0066CC"
- />
TextView 是支持简单的 html 标签的,如 <a> 标签, 但并不是支持所有标签,支持更复杂的 html 标签得用 webView 组件。
android:textColorLink 是设置文字链接的颜色. 虽然 TextView 支持 <a> 标签,但是这里是不能点此链接的,不要被假象迷惑。
区域 3 是一直猫的卡通图片,貌似有点丑,将就下吧:
XML/HTML 代码
- <ImageView android:id="@+id/miniTwitter_logo"
- android:src="@drawable/cat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentBottom="true"
- android:layout_marginRight="25dp"
- android:layout_marginLeft="10dp"
- android:layout_marginBottom="25dp"
- />
android:layout_alignParentRight="true" 位于 layout 的最右边
android:layout_alignParentBottom="true" 位于 layout 的最底部
android:layout_marginRight="25dp" 该 imageView 的边框距离 layout 边框有 25dp, 其他的 margin 类似。
区域 4 是一个带文字的图片的 ImageView:
XML/HTML 代码
- <ImageView android:src="@drawable/logo"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@id/miniTwitter_logo"
- android:layout_alignBottom="@id/miniTwitter_logo"
- android:paddingBottom="8dp"
- />
android:layout_toLeftOf="@id/miniTwitter_logo" 在那个小猫 ImageView 的左边(水平位置)
android:layout_alignBottom="@id/miniTwitter_logo" 这里意思是这两个 ImageView(区域 3 和区域 4) 下边缘对齐
android:paddingBottom="8dp" 图片距离 ImageView 底部边框 8dp,也就是将图片上抬个 8dp
实现 miniTwitter 登陆界面的具体步骤
具体步骤如下:
第一步:一些字符串定义
/miniTwitterLoginDemo/res/values/strings.xml
XML/HTML 代码
- <?xml version="1.0" encoding="utf-8" ?>
- <resources>
- <string name="hello">
- Hello World, LoginActivity!
- </string>
- <string name="login_label_username">
- 帐号
- </string>
- <string name="login_label_password">
- 密码
- </string>
- <string name="login_label_signin">
- 登 录
- </string>
- <string name="login_status_logging_in">
- 登录中...
- </string>
- <string name="login_username_hint">
- Email或手机号
- </string>
- <string name="login_register_link">
- 没有帐号?
- <a href="#" mce_href="#">
- 注册
- </a>
- </string>
- <string name="app_name">
- miniTwitter
- </string>
- </resources>
第二步:
/miniTwitterLoginDemo/res/values/style.xml
XML/HTML 代码
- <?xml version="1.0" encoding="utf-8" ?>
- <resources>
- <style name="normalText" parent="@android:style/TextAppearance">
- <item name="android:textColor">
- #444
- </item>
- <item name="android:textSize">
- 14sp
- </item>
- </style>
- </resources>
第三步:背景色为渐变色
/miniTwitterLoginDemo/res/drawable-mdpi/background_login.xml
XML/HTML 代码
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <gradient
- android:startColor="#FFACDAE5"
- android:endColor="#FF72CAE1"
- android:angle="45"
- />
- </shape>
第四步:背景色味淡蓝色且为圆角
/miniTwitterLoginDemo/res/drawable-mdpi/background_login_div_bg.xml
XML/HTML 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#55FFFFFF" />
- <!-- 设置圆角
- 注意: bottomRightRadius是左下角而不是右下角 bottomLeftRadius右下角-->
- <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"
- android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>
- </shape>
第五步:
/miniTwitterLoginDemo/res/layout/login.xml
XML/HTML 代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/background_login">
- <!-- padding 内边距 layout_margin 外边距
- android:layout_alignParentTop 布局的位置是否处于顶部 -->
- <RelativeLayout
- android:id="@+id/login_div"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="15dip"
- android:layout_margin="15dip"
- android:background="@drawable/background_login_div_bg"
- >
- <!-- 账号 -->
- <TextView
- android:id="@+id/login_user_input"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_marginTop="5dp"
- android:text="@string/login_label_username"
- style="@style/normalText"/>
- <EditText
- android:id="@+id/username_edit"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="@string/login_username_hint"
- android:layout_below="@id/login_user_input"
- android:singleLine="true"
- android:inputType="text"/>
- <!-- 密码 text -->
- <TextView
- android:id="@+id/login_password_input"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/username_edit"
- android:layout_marginTop="3dp"
- android:text="@string/login_label_password"
- style="@style/normalText"/>
- <EditText
- android:id="@+id/password_edit"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/login_password_input"
- android:password="true"
- android:singleLine="true"
- android:inputType="textPassword"
- />
- <!-- 登录button -->
- <Button
- android:id="@+id/signin_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/password_edit"
- android:layout_alignRight="@id/password_edit"
- android:text="@string/login_label_signin"
- android:background="@drawable/blue_button"
- />
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView android:id="@+id/register_link"
- android:text="@string/login_register_link"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="15dp"
- android:textColor="#888"
- android:textColorLink="#FF0066CC"
- />
- <ImageView android:id="@+id/miniTwitter_logo"
- android:src="@drawable/cat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentBottom="true"
- android:layout_marginRight="25dp"
- android:layout_marginLeft="10dp"
- android:layout_marginBottom="25dp"
- />
- <ImageView android:src="@drawable/logo"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@id/miniTwitter_logo"
- android:layout_alignBottom="@id/miniTwitter_logo"
- android:paddingBottom="8dp"
- />
- </RelativeLayout>
- </LinearLayout>
第七步:
/miniTwitterLoginDemo/src/com/mytwitter/acitivity/LoginActivity.java
这里要注意的是,该 Activity 是无标题的,设置无标题需要在 setContentView 之前设置,否则会报错。
Java 代码
- package com.mytwitter.acitivity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Window;
- public class LoginActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.login);
- }
- }
到此,Android 中的 miniTwitter 登录界面的制作就介绍完毕了,是不是做出不错的登录界面并不算难呢?
来源: http://www.phperz.com/article/17/0315/290431.html