Changed to Chinese and English summary.
I. Why is it recommended not to directly preview the Greeting()
function, but instead to add another function (in this case, the BirthdayCardPreview()
function) that calls the Greeting()
function with appropriate parameters?
为什么建议不要直接预览 Greeting() 函数,而是需要添加另一个函数(在本例中为 BirthdayCardPreview() 函数),由该函数使用适当的形参调用 Greeting() 函数?
1.Reusability of Composable Functions
The Greeting()
function is part of the business logic and may need to be used with different parameters in different contexts.
Forcing all parameters to have default values could lead to situations where you have to handle unnecessary default cases in the business logic, which increases code complexity.
Modifying business logic directly for preview purposes (e.g., providing unnecessary default values) would make the code less flexible and consistent.
可组合函数的复用性
Greeting()
函数是业务逻辑的一部分,可能需要在不同场景中使用不同的参数来呈现。
强制为所有参数提供默认值,可能导致你需要在业务逻辑中处理一些无意义的默认情况,增加了代码复杂度。直接为预览服务而修改业务逻辑(比如提供不必要的默认值),会让代码失去灵活性和一致性。
2.Flexibility of Preview
By introducing an intermediate function like BirthdayCardPreview()
, you can customize how the function is called in the preview without affecting the business logic itself.
In BirthdayCardPreview()
, you can manually pass in appropriate parameter values, such as the specific name "Android".
In the future, if more complex preview scenarios (such as dynamic parameters, theme changes, etc.) are required, you can modify BirthdayCardPreview()
without changing Greeting()
.
2.预览的灵活性
通过引入 BirthdayCardPreview()
这样的中间函数,你可以在预览中自定义调用方式,而不会对业务逻辑本身产生影响。
在 BirthdayCardPreview()
中,你可以手动传入适合展示的参数值,比如特定的名字 "Android"。
未来如果需要更复杂的预览场景(比如动态参数、主题变换等),可以通过修改 BirthdayCardPreview()
来实现,而无需修改 Greeting()
。
3.Decoupling Logic
Separating the preview logic from the business logic reduces the invasive impact of preview logic on the business logic, keeping the code clean.
3.解耦逻辑
将预览逻辑与业务逻辑分开,可以减少预览逻辑对业务逻辑的侵入性影响,保持代码清晰。
II. The Significance of Named Arguments
Ⅱ.具名实参的意义
- Improves code readability, especially when the function has multiple parameters.
- Reduces the likelihood of errors, as the order of parameters becomes less important.
提高代码可读性,特别是当函数有多个参数时。
减少出错的可能性,因为参数顺序不再那么重要。
III. Why is it recommended to use increments of 4.dp when setting spacing and padding values in Jetpack Compose?
Ⅲ.为什么Jetpack Compose 中,建议使用 4.dp 的增量来设置间距和内边距值?
1.Grid System
The layout of Material Design is based on a 4dp grid system, where all components' alignments, spacing, and layouts align with this grid.
网格系统
Material Design 的布局基于 4dp 的网格系统,所有组件的对齐、间距和布局都会与这个网格对齐。
2.Cross-Device Adaptability
Android devices vary greatly in screen size and resolution.
Using increments of 4.dp allows for better adaptation to different screen densities and resolutions.
Even when zooming in or out the layout, the spacing remains natural and balanced.
2.跨设备适配
Android 设备屏幕尺寸和分辨率差异很大
以 4dp 为增量,能够更好地适配各种屏幕密度和分辨率。
即使在放大或缩小布局时,间距仍然显得自然且平衡。
3.Material Design Recommended Values
Commonly recommended spacing values are usually multiples of 4.dp, for example:
- 4.dp: for fine spacing (e.g., between icons and text).
- 8.dp: for slightly larger spacing (e.g., padding inside cards).
- 16.dp: for larger spacing (e.g., between components).
- 24.dp and 32.dp: for even larger spacing (e.g., screen margins or gaps between groups of components).
3.Material Design实际推荐值
推荐的常用间距值通常是 4.dp 的倍数,例如:
4.dp:用于细微的间距(如图标与文字之间)。
8.dp:用于稍大的间距(如卡片的内边距)。
16.dp:用于较大的间距(如组件之间的分隔)。
24.dp 和 32.dp:用于更大层次的间距(如屏幕边距或组件组之间的间隔)。
IV. Types of Composables
Ⅳ.可组合项分类
1.Basic Composables
Compose provides some built-in composables, such as:
Text
: for displaying text.Button
: for responding to click events.Image
: for displaying images.Column
andRow
: for layout management.
1.基础可组合项
Compose 提供了一些内置的可组合项,比如:
Text
:用于显示文字。
Button
:用于响应点击事件。
Image
:用于显示图片。
Column
和 Row
:用于布局管理
2.Custom Composables
Developers can create their own composables based on their needs.
2.自定义可组合项
开发者可以基于需求创建自己的可组合项。
3.Composable Composition
Composables can be nested or composed together to build complex user interfaces.
3. 组合可组合项
可组合项可以嵌套或组合使用来构建复杂界面。
V. Core App Development Process
Ⅴ.app核心开发流程
- Requirement Analysis and Architecture Design
- Frontend Development (UI and Interaction Logic)
- Backend Development (API and Business Logic)
- Database Development (Local and Remote Storage)
- Frontend and Backend Communication
- Testing and Debugging
- Deployment and Release
1.需求分析和架构设计
2.前端开发(UI 和交互逻辑)
3.后端开发(API 和业务逻辑)
4.数据库开发(本地与远程存储)
5.前后端通信
6.测试与调试
7.部署与发布
VI. What is Retrofit?
Ⅵ.Retrofit 是什么?
Retrofit is a type-safe HTTP client library that simplifies the process of interacting with the network in Android and Java applications. Retrofit is one of the libraries recommended by Google for network requests and is widely used for network communication.
Retrofit 是一个 类型安全的 HTTP 客户端库,它简化了 Android 和 Java 应用程序中与网络进行交互的过程。Retrofit 是 Google 推荐的网络请求库之一,广泛用于网络通信。
Features:
-
Simple and Easy to Use:
Retrofit’s API is designed to be simple and intuitive to use. -
Automatic Parsing:
Retrofit supports automatically converting server response data (e.g., JSON) into Java or Kotlin objects. Common conversion libraries include Gson or Moshi. -
Asynchronous Requests:
Retrofit supports both synchronous and asynchronous requests and can be used with Coroutines or RxJava. -
Supports Various HTTP Methods:
Retrofit supports common HTTP methods such as GET, POST, PUT, DELETE, etc. -
Easy Integration:
Retrofit integrates seamlessly with OkHttp (a lower-level HTTP client) and supports middleware interception, request retries, caching, and other features.
特点:
1.简单易用: Retrofit 的 API 设计简单,使用起来非常直观。
2.自动解析: Retrofit 支持将服务器返回的数据(如 JSON)自动转换为 Java 或 Kotlin 对象。常用的转换库有 Gson 或 Moshi。
3.支持异步请求: Retrofit 支持同步和异步请求,并且能够与 协程(Coroutines) 或 RxJava 配合使用。
4.支持各种 HTTP 方法: 支持常见的 HTTP 方法,如 GET、POST、PUT、DELETE 等。
5.集成方便: 与 OkHttp(一个低层次的 HTTP 客户端)无缝集成,支持中间件拦截、请求重试、缓存等功能。