Android string.xml 通配符 %$用法

格式化字符串详解
本文介绍了一种格式化字符串的方法,并提供了具体的属性值示例。通过使用特定的格式化字符串,可以方便地插入变量值,例如用户名和邮件计数。文中还展示了如何在程序中调用这些格式化字符串。

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

在这个例子中,这个格式化的字符串有2个参数

属性值举例说明
%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格 
%n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0 
%n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00

 

在程序中按照下面的方法来根据参数来格式化字符串:

Resources res = getResources();

String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: reflect bootstrap failed: 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: java.lang.reflect.InvocationTargetException 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at java.lang.reflect.Method.invoke(Native Method) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at org.hapjs.common.utils.hiddenapi.BootstrapClass.<clinit>(SourceFile:79) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at org.hapjs.common.utils.hiddenapi.Reflection.unseal(SourceFile:20) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at org.hapjs.common.utils.hiddenapi.Reflection.invoke(SourceFile:9) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at com.nearme.instant.platform.Application.attachBaseContext(SourceFile:45) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.Application.attach(Application.java:368) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.Instrumentation.newApplication(Instrumentation.java:1368) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1566) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.LoadedApk.makeApplicationInner(LoadedApk.java:1479) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:8574) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2792) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.os.Handler.dispatchMessage(Handler.java:115) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.os.Looper.loopOnce(Looper.java:298) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.os.Looper.loop(Looper.java:408) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at android.app.ActivityThread.main(ActivityThread.java:9952) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at java.lang.reflect.Method.invoke(Native Method) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:613) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1074) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: Caused by: java.lang.NoSuchMethodException: dalvik.system.VMRuntime.setHiddenApiExemptions [class [Ljava.lang.String;] 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at java.lang.Class.getMethod(Class.java:2940) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: at java.lang.Class.getDeclaredMethod(Class.java:2919) 08-19 16:07:04.408 10153 13992 13992 W BootstrapClass: ... 19 more 08-19 16:07:04.410 10106 6568 13904 W SyncManager: sync resultDto: InstalledResultDto{id='472', code=200, message='null', ext=null} 08-19 16:07:04.419 root 827 827 D Zygote : Forked child process 14184 日志分析
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值