一、 安卓开发的常用布局
1、线性布局(LinearLayout)
(1) 特点:主要以水平或垂直方式来显示界面中的控件。当控件水平排列时,显示顺序依次为从左到右,当控件垂直排列时,显示顺序依次为从上到下。
注:子控件不可重叠,但可设置权重
(2) 使用场景:需要线性排列元素的界面,例如:登录界面。
(3) 示例代码:
<?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:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal"
android:background="@drawable/bg1"
>
<!-- 标题 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="30dp"/>
<!-- 用户名输入框 -->
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:inputType="text"
android:layout_marginTop="24dp"/>
<!-- 密码输入框 -->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:layout_marginTop="12dp"/>
<!-- 登录按钮 -->
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginTop="24dp"/>
<!-- 注册链接 -->
<TextView
android:id="@+id/textViewRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="没有账户?注册"
android:layout_marginTop="12dp"
android:textColor="@android:color/holo_red_light" />
</LinearLayout>
2、约束布局(ConstraintLayout)
(1) 特点:可以在横向和纵向上以添加约束关系的方式进行相对定位。
(2) 使用场景:需要复杂布局和动态调整的界面,例如:底部功能按钮。
(3) 示例代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1"
>
<!-- 标题 -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="50dp"
/>
<!-- 用户名输入框 -->
<EditText
android:id="@+id/editTextUsername"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="用户名"
android:inputType="text"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="24dp"/>
<!-- 密码输入框 -->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@+id/editTextUsername"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="12dp"/>
<!-- 登录按钮 -->
<Button
android:id="@+id/buttonLogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="登录"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="24dp"/>
<!-- 注册链接 -->
<TextView
android:id="@+id/textViewRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="没有账户?注册"
app:layout_constraintTop_toBottomOf="@+id/editTextPassword"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="12dp"
android:textColor="@android:color/holo_red_light" />
</androidx.constraintlayout.widget.ConstraintLayout>
3、表格布局(TableLayout)
(1) 特点:表格形式展示数据,可以自动处理列宽以及拉伸特定的列。
(2) 使用场景:具有固定列数的数据,或输入表单数据。
(3) 示例代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="@drawable/bg1"
>
<!-- 表单标题 -->
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录表单"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<!-- 空行 -->
<TableRow>
<View
android:layout_width="match_parent"
android:layout_height="16dp" />
</TableRow>
<!-- 用户名 -->
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</TableRow>
<!-- 密码 -->
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
</TableRow>
<!-- 确认密码 -->
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:" />
<EditText
android:id="@+id/editTextConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请再次输入密码"
android:inputType="textPassword" />
</TableRow>
</TableLayout>
4、帧布局(FrameLayout)
(1) 特点:子控件可进行重叠,默认最后子控件位于最顶层。
(2) 使用场景:菜单的弹出或图文的重叠。
(3) 示例代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light">
<!-- 第一个叠加层:背景图片 -->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg1"
android:scaleType="centerCrop" />
<!-- 第二个叠加层:页面内容 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:layout_gravity="center">
<!-- 页面标题 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帧布局"
android:textSize="24sp"
android:textStyle="bold" />
<!-- 页面内容 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FrameLayout"
android:textSize="16sp" />
<!-- 按钮 -->
<Button
android:id="@+id/buttonAction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击我" />
</LinearLayout>
<!-- 第三个叠加层:浮动按钮 -->
<ImageButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:background="@android:color/holo_blue_light"
android:layout_gravity="bottom|end"
android:contentDescription="浮动操作按钮" />
</FrameLayout>
5、相对布局(RelativeLayout)
(1) 特点:相对于其他视图的位置进行布局。
(2) 使用场景:界面视图有明显的关系,例如:输入框之间的联系。
(3) 示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@drawable/bg1"
>
<!-- 标题 -->
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="24sp"
android:textStyle="bold"
android:layout_alignParentTop="true"/>
<!-- 用户名输入框 -->
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:layout_below="@+id/textViewTitle"
android:layout_marginTop="16dp"/>
<!-- 密码输入框 -->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:layout_below="@+id/editTextUsername"
android:layout_marginTop="16dp"/>
<!-- 登录按钮 -->
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:layout_below="@+id/editTextPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"/>
</RelativeLayout>
二、 常用UI界面交互功能
1、按钮点击事件
(1) 实现方法:使用setOnClickListener绑定按钮事件监听器。
(2) 实际案例:点击不同的三个按钮实现不同的效果:
a. 点击submit按钮将输入文本框内容打印到界面中间;
b. 点击reset按钮将输入文本框内容清空并打印清理成功;
c. 点击cancel按钮退出应用。
(3) 示例代码:
<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="@drawable/bg1"
tools:context=".MainActivity"
>
<!-- 标题 -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="50dp"
/>
<!-- 用户名输入框 -->
<EditText
android:id="@+id/editTextUsername"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="用户名"
android:inputType="text"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="24dp"/>
<!-- 密码输入框 -->
<EditText
android:id="@+id/editTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@+id/editTextUsername"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="12dp"/>
<!-- 提交按钮 -->
<Button
android:id="@+id/buttonSubmit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="提交"
app:layout_constraintTop_toBottomOf="@+id/editTextPassword"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/buttonReset"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp" />
<!-- 重置按钮 -->
<Button
android:id="@+id/buttonReset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="重置"
app:layout_constraintTop_toTopOf="@+id/buttonSubmit"
app:layout_constraintLeft_toRightOf="@+id/buttonSubmit"
app:layout_constraintRight_toLeftOf="@+id/buttonCancel"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<!-- 取消按钮 -->
<Button
android:id="@+id/buttonCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="取消"
app:layout_constraintTop_toTopOf="@+id/buttonSubmit"
app:layout_constraintLeft_toRightOf="@+id/buttonReset"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp" />
<!-- 用于显示打印信息的TextView -->
<TextView
android:id="@+id/textViewInfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/buttonSubmit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
android:textSize="18sp"
android:visibility="gone" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- MainActivity-->
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername;
private EditText editTextPassword;
private Button buttonSubmit;
private Button buttonReset;
private Button buttonCancel;
private TextView textViewInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonSubmit = findViewById(R.id.buttonSubmit);
buttonReset = findViewById(R.id.buttonReset);
buttonCancel = findViewById(R.id.buttonCancel);
textViewInfo = findViewById(R.id.textViewInfo);
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 打印用户名和密码到界面中间
String infoText = "用户名: " + editTextUsername.getText() + "\n密码: " + editTextPassword.getText();
textViewInfo.setText(infoText);
textViewInfo.setVisibility(View.VISIBLE);
}
});
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 清空输入框并打印"清理成功"
editTextUsername.setText("");
editTextPassword.setText("");
textViewInfo.setText("清理成功");
textViewInfo.setVisibility(View.VISIBLE);
}
});
buttonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 退出应用
finishAffinity(); // 关闭整个应用
}
});
}
}
相关效果图:
三、改进措施
1. 设定明确的学习目标
我为自己设定了短期和长期的学习目标。短期目标可能包括掌握某个特定的Android API或开发一个简单的应用功能,而长期目标则是成为一名熟练的Android开发者。
2. 深入学习基础知识
我意识到扎实的基础是成为一名优秀开发者的关键。因此,我专注于深入学习Java或Kotlin编程语言、Android SDK、UI/UX设计原则以及数据库和网络编程等基础知识。
3. 积极参与实践项目
我通过参与实际项目来应用所学知识。这些项目可以是自己的个人项目,也可以是开源项目或实习项目。通过实践,我能够更好地理解理论知识,并发现自己在开发过程中的不足。
4. 寻求反馈并持续改进
在完成项目后,我积极向导师、同事或社区成员展示我的作品,并接受他们的反馈。这些反馈帮助我识别代码中的错误、改进应用的功能和设计,并提升我的开发技能。
5. 积极参与技术社区
我加入了多个Android开发社区,如Stack Overflow、GitHub和Reddit等。在这些社区中,我可以向其他开发者请教问题、分享我的经验和见解,并从他们的回答和建议中受益。
6. 养成编写代码文档的习惯
我意识到编写清晰、准确的代码文档对于项目的成功至关重要。因此,我养成了为我的代码编写注释、编写用户手册和开发者指南的习惯。这不仅有助于他人理解我的代码,也使我能够更好地组织和管理我的工作。
四、总结
回顾本学期的学习历程,我深感自己在APP开发领域取得了不小的进步。但同时,我也意识到自己在某些方面还有待提高,如代码规范、文档编写等。在未来的学习和工作中,我将继续努力,不断提高自己的能力和水平。通过本学期的学习,我对APP开发领域有了更深入的了解和认识。我相信,在未来的学习和工作中,我将能够运用所学知识,开发出更多优秀的应用程序,并为用户带来更好的体验。同时,我也将保持持续学习的态度,不断跟进最新的技术动态和发展趋势,为自己的职业生涯奠定坚实的基础。
A23计科7班林忠佳