实验二:界面设计:布局管理器
一、 实验目的
1. 了解四种布局管理器的区别和各自特有的属性。
2. 掌握四种布局管理器的应用场合和用法
3. 灵活使用四种布局管理器的嵌套实现各自复杂布局
4. 掌握复用XML布局文件的方法
5. 掌握代码控制UI界面
二、 实验内容
请按照图1设计的样式,完成如图2的Android应用UI的开发
三、 理论
根据实验内容及要求,将涉及到的相关理论方面的知识点在此处列举一下。
布局管理器能够管理Android应用中用户UI的各种视图组件,Android提供了四种常用布局管理器,即线性布局 LinearLayout、表格布局 TableLayout、帧布局 FrameLayout 和相对布局RelativeLayout,另外在设计UI布局时,布局管理器能够嵌套,对于相同的布局可以通过和标签进行布局文件的复用。为了提高布局的灵活性,Android还提供了使用代码控制UI页面,以及代码和XML联合控制UI界面的方法。
四、 设计步骤
将设计内容细化为具体步骤,完善每步的具体内容。
-
用java代码设置全屏
打开工程src目录下的Activity文件,在onCreate方法中执行语句super.onCreate(savedInstanceState)之前,添加如下两条语句: -
按照图1设计的样式,完成一个Android应用UI的开发
(1) 将主布局修改为线性布局,如下代码所示,请在LinearLayout标签内添加背景色和布局方向。
(2) 在主布局中添加一个显示logo的ImageView组件,可根据实际屏幕大小设置图片宽高,请完成如下代码中的提示部分。
(3) 在主布局中继续添加一个线性布局,该布局分成两列,分别放置头像和表格布局,表格布局也分为两行,放置用户名和密码,请完成如下代码的提示部分。
(4) 在主布局中继续添加一个登录按钮,请完成如下代码的提示部分。
(5) 在主布局中继续添加三个已选中的复选框,分别为记住密码,自动登录和接收产品推广,请完成如下代码的提示部分。
(6) 在主布局中继续添加一个布局管理器,该布局管理中放置忘记密码和注册账号两个按钮,请完成如下代码中的提示部分。
(7) 在主布局中继续添加一个布局管理器,该布局管理器中放置进度条和表示加载中的文本框,请完成如下代码的提示部分。
(8) 在主Activity文件中,添加用于登录的login方法,用于处理忘记密码的forgetPass方法,用于打开组成界面register方法,请完成如下代码的提示部分。
五、 效果图展示
根据实验内容的要求,把设计的每部分效果图进行展示,并对核心代码进行分析。(此处需要截图)
代码块
有一些注意点!!!
这里适用于不会一些创建操作的童鞋,会的可以划走
建立新的活动
下拉框选择project视图,在main里新建一个空活动
一定要勾选Generate a layout File,因为这样他就会帮你自动创建布局文件了,省去了很多麻烦的操作
你创建活动时会同时在清单文件中进行注册
如果想切换主活动的话,就把这个注册为主活动的代码(如下代码块)给拖动到想设置成主活动的代码中,然后把android:exported=“false”>设置成android:exported=“true”>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
再来说一说插入图片吧
我们平常插入图片的位置
复制文件,然后右键paste(粘贴)
最后代码中引用
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:background="#fffae4"
tools:context=".MainActivity">
<ImageView
android:layout_width="1070px"
android:layout_height="381px"
android:layout_gravity="center_horizontal"
android:src="@drawable/logo"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
>
<ImageView
android:layout_width="120dp"
android:layout_height="100dp"
android:src="@drawable/spa"
/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:text="账号:"
android:textSize="30dp"
/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/userName"
/>
</TableRow>
<TableRow>
<TextView
android:text="密码:"
android:textSize="30dp"
/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/userPass"
/>
</TableRow>
</TableLayout>
</LinearLayout>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="登 录"
android:onClick="login"
/>
<CheckBox
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="20dp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:textSize="20dp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接受产品推广"
android:textSize="20dp"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码"
android:onClick="forgetPass"
android:layout_gravity="left"
android:layout_marginLeft="80dp"
android:layout_marginTop="5dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册账号"
android:onClick="register"
android:layout_gravity="right"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
/>
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
>
<ProgressBar
android:layout_width="30dp"
android:layout_height="30dp"
style="?android:attr/progressBarStyleSmall"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..."
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</LinearLayout>
MainActivity…java
package com.example.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
public void login(View v) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
String s = "成功登录";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
startActivity(intent);
}
public void forgetPass(View v) {
Intent intent = new Intent(MainActivity.this, ForgetActivity.class);
String s = "请修改密码";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
startActivity(intent);
}
public void register(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
String s = "请完成注册";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
startActivity(intent);
}
}
activity_forget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ForgetActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请重新填写密码进行修改"
android:textSize="30dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="30dp"
/>
<EditText
android:id="@+id/userPass"
android:layout_width="400dp"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"
android:textSize="30dp"
/>
<EditText
android:layout_width="400dp"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成修改"
android:onClick="forgetPass"
android:layout_gravity="center"
/>
</LinearLayout>
ForgetActivity.java
package com.example.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class ForgetActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forget);
}
public void forgetPass(View v) {
Intent intent = new Intent(ForgetActivity.this, MainActivity.class);
String s = "完成修改密码,请重新登录";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
startActivity(intent);
}
}
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".RegisterActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请填写账号密码进行注册"
android:textSize="30dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="30dp"
/>
<EditText
android:id="@+id/userName"
android:layout_width="400dp"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="30dp"
/>
<EditText
android:id="@+id/userPass"
android:layout_width="400dp"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"
android:textSize="30dp"
/>
<EditText
android:layout_width="400dp"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成注册"
android:onClick="register"
android:layout_gravity="center"
/>
</LinearLayout>
RegisterActivity.java
package com.example.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void register(View v) {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
String s = "完成注册,请再次登录";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
startActivity(intent);
}
}
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".LoginActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="成功登录了!"
android:layout_gravity="center"
android:gravity="center"
android:textSize="50dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/spainfo"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="重新登陆"
android:onClick="login"
/>
</LinearLayout>
LoginActivity.java
package com.example.test2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void login(View v) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
String s = "请重新输入账号密码进行登录";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
startActivity(intent);
}
}
制作不易,如有问题的话可以私信我,客官如果不嫌麻烦的话点个赞吧