Android学习总结
安卓开发中各种布局
View与ViewGroup
View是屏幕上的基本构建块,它可以是文本、图像、按钮或其他任何东西。每个View都有自己的大小、位置和形状。View具有自己的属性和方法,可以设置颜色、字体、背景等等。View是安卓所有控件的父类,因此所有的UI组件都是View的子类
ViewGroup是一组View的容器,它用于组合多个View。它可以是线性布局、相对布局、帧布局等等。ViewGroup可以包含其他View和ViewGroup,形成嵌套结构。ViewGroup的职责是管理其子View的布局,包括子View的大小、位置和顺序。ViewGroup是View的直接子类,所以TextView和LinearLayout都是一个View
常用布局
线性布局
Android线性布局(LinearLayout)是一种常见的布局方式,它允许开发者将界面中的控件沿着一条轴线(水平或垂直)排列。在线性布局中,控件可以按照顺序排列,形成一条直线。
线性布局特点
- 方向性:线性布局可以设置为水平(horizontal)或垂直(vertical)排列。
- 布局属性:线性布局有几个重要的属性,包括 orientation(控制布局方向)、gravity(控制布局内控件的对齐方式)、layout_weight(控制控件在剩余空间中的分配比例)等。
- 灵活性:通过调整这些属性,开发者可以灵活地控制界面布局的外观和行为。
代码预览
<?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=".MainActivity">
<Button
android:id="@+id/bt1"
android:text="按钮一"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bt2"
android:text="按钮二"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout
代码效果预览
约束布局
约束布局(ConstraintLayout)是Android平台上的一种布局管理器,它允许开发者通过定义组件之间的约束关系来布局界面,而不是像传统的线性布局或相对布局那样通过嵌套布局来实现。约束布局的出现主要是为了解决布局嵌套过多导致的性能问题,以及提高布局的灵活性和可维护性。
约束布局特点
- 减少布局嵌套:约束布局通过定义组件之间的约束关系,避免了不必要的嵌套,从而减少了布局层次,提高了渲染效率。
- 灵活的布局方式:约束布局可以通过定义组件之间的相对位置关系,实现复杂的布局设计,如对称、动态调整等。
- 支持多种屏幕尺寸:约束布局可以根据屏幕的宽度和高度动态调整组件的大小和位置,适应不同屏幕尺寸。
- 易于维护:由于约束布局减少了布局层次,使得布局更加清晰易读,便于后期维护和修改。
代码预览
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/bt1"
android:text="按钮一"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/bt2"
android:text="按钮二"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
app:layout_constraintTop_toBottomOf="@+id/bt1" />
</androidx.constraintlayout.widget.ConstraintLayout>
代码效果预览
帧布局
帧布局(FrameLayout)是Android中的一种布局管理器,它允许开发者在屏幕上叠加显示视图。这种布局方式特别适合创建叠加式的UI布局,例如屏幕上方的通知栏。在FrameLayout中,每个控件都覆盖在其他控件之上,因此最后添加到布局中的控件将位于布局的顶部,并覆盖在其他控件之上。
帧布局特点
- 简单性:FrameLayout是六大布局中最为简单的一个布局,它直接在屏幕上开辟出一块空白的区域,当向其中添加控件时,默认将它们放置在该区域的左上角。
- 灵活性:尽管默认将控件放置在左上角,但可以通过layout_gravity属性指定控件在布局中的对齐方式,类似于LinearLayout中的用法。
- 大小决定:FrameLayout的大小由控件中最大的子控件决定。如果控件大小相同,则只能看到最上面的组件。后续添加的控件会覆盖前一个。
- 前景图像:FrameLayout支持设置前景图像,这是永远处于最上层的图像,不会被其他视图覆盖。可以通过android:foreground属性设置前景图像,以及android:foregroundGravity属性设置前景图像的显示位置。
代码预览
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/图片1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="叠加文案"
android:textColor="#FF00FF"
android:textSize="50dp"
tools:ignore="MissingConstraints"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
代码效果预览
表格布局
在Android开发中,表格布局(TableLayout)是一种通过行和列的形式来组织界面组件的布局方式。它类似于HTML中的表格结构,允许开发者创建具有固定行和列的布局。每个行通常由一个TableRow对象表示,而列的数量则由包含最多控件的TableRow决定。
表格布局特点
- 结构化布局:表格布局允许开发者通过行和列的方式来组织界面元素,使得布局更加有序和清晰。
- 灵活性:开发者可以通过设置每行显示的列数,以及使用TableRow视图来定义每行的高度和宽度,从而实现不同的布局效果。
- 可扩展性:表格布局支持动态添加或删除行和列,便于适应不同的界面需求。
- 属性控制:表格布局提供了一系列属性来控制布局的行为,例如android:columnCount用于指定表格的列数,android:stretchColumns用于指定哪些列可以伸展以填充额外空间,android:collapseColumns用于隐藏特定的列等。
- 单元格属性:除了全局属性之外,表格布局还支持单元格级别的属性,如android:layout_column用于指定单元格在哪一列显示,android:layout_span用于指定单元格跨越的列数。
- 兼容性:表格布局是Android早期版本中就支持的布局方式,因此具有良好的向下兼容性。
代码预览
<?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="1">
<TableRow>
<TextView
android:text="姓名"
android:padding="3dip" />
<TextView
android:text="张三"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="年龄"
android:padding="3dip" />
<TextView
android:text="25"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="职业"
android:padding="3dip" />
<TextView
android:text="软件工程师"
android:padding="3dip" />
</TableRow>
</TableLayout>
代码效果预览
相对布局
相对布局(RelativeLayout)是Android中一种常用的布局方式,它允许开发者根据父容器和兄弟控件的位置来确定控件的位置。这种布局方式具有很大的灵活性,可以很好地处理复杂的界面设计需求。
相对布局特点
- 灵活性:相对布局可以灵活地安排控件的位置,支持控件在水平、垂直方向上的对齐、居中等布局方式。
- 相对位置:控件可以根据其他控件的位置来确定自己的位置,可以使用控件的ID或者在布局文件中声明的其他控件来描述相对位置关系。
- 嵌套使用:相对布局可以嵌套使用,即一个相对布局里面可以包含另一个相对布局。
- 复杂布局:相对布局可以用来实现复杂的布局效果,例如一个控件相对于另一个控件的位置是根据屏幕大小和分辨率动态计算的。
- 可读性:相对布局可以使布局文件更加易读易懂,因为可以通过命名和描述控件之间的相对位置关系来实现布局。
代码预览
<?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">
<Button
android:id="@+id/左上角按钮"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左上角按钮"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"/>
<Button
android:id="@+id/右上角按钮"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上角按钮"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="居中文字"
android:textSize="24dp"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/置底按钮"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="置底按钮"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
代码效果预览
UI界面交互功能的实现
按钮点击事件
在Android开发中,实现按钮点击事件的基本流程包括以下几个步骤:
- 定义按钮:在XML布局文件中定义按钮,并赋予其唯一的ID。
- 获取按钮引用:在Activity或Fragment中通过findViewById()方法获取到按钮的引用。
- 设置点击监听器:为按钮设置OnClickListener监听器,这个监听器可以是匿名内部类、实现了OnClickListener接口的类实例,或者是Activity本身实现了OnClickListener接口。
- 实现点击事件处理:在OnClickListener的onClick()方法中编写具体的点击事件处理逻辑。
代码预览
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerInParent="true"/>
// 通过ID找到按钮
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 当按钮被点击时执行的操作
Toast.makeText(MainActivity.this, "按钮被点击了!", Toast.LENGTH_SHORT).show();
}
});
列表项点击事件
-
定义列表组件
-
设置适配器
-
设置点击事件监听器
-
编写点击事件处理逻辑
代码预览
<?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"
tools:context=".MainActivity">
<ListView
android:id="@+id/taskList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView taskList = findViewById(R.id.taskList);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[]{"任务1", "任务2"});
taskList.setAdapter(adapter);
taskList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String task = (String) parent.getItemAtPosition(position);
Log.d("MainActivity", "任务 " + task + " 被点击");
}
});
}
}
代码效果预览
滑动操作
- 垂直滚动视图 Android中用于显示超出屏幕高度的内容,并允许用户通过垂直滑动来查看全部内容的视图。它通常包含一个子视图(可以是任何视图或视图组),当子视图的内容高度超过
ScrollView的显示区域时,ScrollView会自动添加滚动条,允许用户上下滑动以查看全部内容。
代码预览
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
- 水平滚动视图用于显示超出屏幕宽度的内容,并允许用户通过水平滑动来查看全部内容的视图。它通常包含一个子视图(可以是任何视图或视图组),当子视图的内容宽度超过
HorizontalScrollView的显示区域时,HorizontalScrollView会自动添加水平滚动条,允许用户左右滑动以查看全部内容。
代码预览
<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</HorizontalScrollView>
菜单项
在Android开发中,菜单项(MenuItems)是用户界面中的一个重要部分,它允许用户通过点击或长按来触发特定的操作。Android提供了两种主要的菜单类型:选项菜单(OptionsMenu)和上下文菜单(ContextMenu)。
布局文件代码 (activity_main.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"
tools:context=".MainActivity">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</RelativeLayout>
菜单资源文件 (menu_main.xml) 在 res/menu 目录下
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_home"
android:title="首页"
android:icon="@drawable/ic_home"
app:showAsAction="always" />
<item
android:id="@+id/action_profile"
android:title="个人中心"
android:icon="@drawable/ic_profile"
app:showAsAction="always" />
</menu>
布局文件 (activity_main.xml)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_home) {
// 切换到首页
Log.d("MainActivity", "切换到首页");
return true;
} else if (id == R.id.action_profile) {
// 切换到个人中心
Log.d("MainActivity", "切换到个人中心");
return true;
}
return super.onOptionsItemSelected(item);
}
}
对话框
Android对话框是应用程序与用户进行交互的重要组件,它允许程序在当前界面弹出一个模式窗口,以获取用户的输入或通知用户某些信息,而不需要离开当前上下文。对话框的设计旨在打断用户的正常操作流程,要求用户先对其进行响应,然后才能继续其他操作。
布局文件 (activity_main.xml)
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置设置" />
活动文件 (MainActivity.java)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button resetButton = findViewById(R.id.resetButton);
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showResetConfirmationDialog();
}
});
}
private void showResetConfirmationDialog() {
new AlertDialog.Builder(this)
.setTitle("确认重置")
.setMessage("您确定要重置设置吗?此操作不可撤销。")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 执行重置操作
performReset();
}
})
.setNegativeButton("取消", null)
.show();
}
private void performReset() {
// 重置设置的逻辑
Log.d("MainActivity", "设置已重置");
}
}
学习过程反思与持续改进措施
在学习过程中,我遇到了一些挑战,但通过查阅官方文档、参与讨论和实践操作,我逐步掌握了各种UI交互功能的实现方法。为了持续提高我的技能水平,我会:
- 查阅文档: 定期阅读安卓官方文档和开发者博客,了解最新的开发技术和最佳实践。
- 参与讨论: 积极参与开发者社区的讨论,向有经验的开发者请教问题,分享自己的经验。
- 实践操作: 多进行项目实践,通过实战积累经验,解决实际开发中遇到的问题。
曾经遇到过的问题
- 示例分析:在实现按钮点击事件时,我遇到的问题是如何在多个按钮之间共享点击处理逻辑。通过研究,我发现可以使用匿名内部类或Lambda表达式来简化代码结构。最终,我选择了Lambda表达式,因为它使代码更加简洁明了。
ini
button1.setOnClickListener(v -> handleButtonClick());
button2.setOnClickListener(v -> handleButtonClick());
- 解决方案解释:选择Lambda表达式是因为它简化了匿名内部类的写法,使代码更清晰,易于维护。同时,它是现代Java开发的推荐方式。