记Android字符串资源支持的参数类型

前言

参数以%开头,后拼接对应的参数类型名称,如下所示:

XML 复制代码
<string name="tips">Hello, %s! You have some new messages.</string>
类型名称如下所示(红色字体为常用类型)

|--------|---------|----------------------------------------------------------------------------|
| s | 字符串格式 | 用于插入字符串值。例如,"Hello, %s!",其中 %s 将被替换为一个字符串。 |
| d | 十进制整数格式 | 用于插入十进制整数。例如,"You have %d messages",其中 %d 将被替换为一个整数值。 |
| f | 浮点数格式 | 用于插入浮点数。例如,"The price is %.2f dollars",其中 %.2f 表示浮点数将被格式化为两位小数。 |
| b | 布尔值格式 | 用于插入布尔值。例如,"Is it true? %b",其中 %b 将被替换为布尔值。 |
| c | 字符格式 | 用于插入字符。例如,"The first letter is %c",其中 %c 将被替换为一个字符。 |
| x | 十六进制格式 | 用于插入十六进制整数。例如,"The color code is #%x",其中 %x 将被替换为一个十六进制整数。 |
| **n** | 参数索引格式 | 允许按索引指定要格式化的参数。例如,`"%1s and %2s"`,其中 `%1s%2$s` 分别表示第一个和第二个参数的字符串值。 |

注意事项

如果在字符串资源文件中声明的字符串内容包含需要在界面中展示的百分号(%),我们需要在该百分号处再拼接一个百分号用于转译。

比如我们有个页面需要展示一个百分比的数据,就会涉及该情况。

参数的使用

具体这些类型在项目中的实际使用请参考安卓官方文档

相关推荐
Kapaseker13 小时前
Kotlin Flow 的 emit 和 tryEmit 有什么区别
android·kotlin
好好学习啊天天向上13 小时前
Android Studio 撕开安卓手机投屏
android·智能手机·android studio
Android-Flutter14 小时前
android - JPG图片转换HDR图片,heic格式
android
诸神黄昏EX1 天前
Android Build系列专题【篇四:编译相关语法】
android
雨白1 天前
优雅地处理协程:取消机制深度剖析
android·kotlin
leon_zeng01 天前
更改 Android 应用 ID (ApplicationId) 后遭遇记
android·发布
2501_916007471 天前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
Jeled1 天前
Retrofit 与 OkHttp 全面解析与实战使用(含封装示例)
android·okhttp·android studio·retrofit
ii_best1 天前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器
2501_915909061 天前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview