一、要求:设计如下图所示的界面
二、涉及知识点
(1)利用到布局的嵌套实现复杂界面
(2)居中对齐,利用线性布局的gravity属性
(3)标签、编辑框、按钮三种控件
(4)编辑框的提示信息,利用hint属性
三、实现步骤:
1、创建安卓应用Login
2、准备背景图片,拷贝到res下的mipmap目录里
3、字符串资源文件strings.xml
<resources>
<string name="app_name">用户登录</string>
<string name="user_login">用户登录</string>
<string name="user">用户:</string>
<string name="user_hint">请输入用户名</string>
<string name="password">密码:</string>
<string name="password_hint">请输入密码</string>
<string name="login">登录</string>
<string name="cancel">取消</string> </resources>
4、布局文件activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@mipmap/background">
<TextView
android:id="@+id/tv_user_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/user_login"
android:textSize="30dp"
android:textColor="#2ea1a8"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:textSize="20dp"/>
<EditText
android:id="@+id/edt_user"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/user_hint"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password"
android:textSize="20dp"/>
<EditText
android:id="@+id/edt_password"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="@string/password_hint"
android:ems="10"
android:inputType="textPassword"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"/>
</LinearLayout>
</LinearLayout>
5、登录界面类LoginActivity
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
6、在项目清单文件里设置启动Activity
如上,登录界面就做好了。