Android TextView 多行格式化

标签 android textview string-formatting multiline

我的 logcat 中的这两行是用 String.fortmat() 格式化的字符串

04-19 13:57:44.918 26962-26962/ts.kiosk.app.checkout E/java.lang.StackTraceElement: "Tendered Amount:                    Rs.     0.00"
04-19 13:57:44.919 26962-26962/ts.kiosk.app.checkout E/java.lang.StackTraceElement: "Change Due:                         Rs.     0.00"

您可以看到这两行的长度完全相同,但在 TextView 中,字符串与它们的格式不完全相同:

String.format("%-25s Rs. %2$8.2f", "Tendered Amount:", formatCentsToCurrency(checkoutBasket.tenderedAmount));

String.format("%-25s Rs. %2$8.2f", "Change Due:", formatCentsToCurrency(checkoutBasket.changeDue));

这就是它在 TextView 中的对齐方式:

"Tendered Amount:                       Rs.     0.00"
"Change Due:                         Rs.     0.00"

我在想我的 TextView 以某种方式弄乱了格式:

                       <TextView
                        android:id="@+id/text_receipt"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        style="@style/popUpReceiptText"
                      android:text="@string/popup_detail_description_default"
                        android:gravity="left"
                        android:layout_gravity="center_horizontal"/>

actual TextEdit

最佳答案

对我来说是正确的答案,实际上如此简单我应该早点发现:

我只是补充说:

android:typeface="monospace"

到我的 TextView

关于Android TextView 多行格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36730823/

相关文章:

java - Android Volley 忽略参数无法连接

java - 格式化为String()

python - 在 python 中,为什么引用字符串格式的百分号和 s 之间的值

python - 如何使用 .format() 打印字符串,并在替换的字符串周围打印文字花括号

android - 房间数据库 Livedata getValue() 返回 null

android - Android Studio如何构建所有Gradle buildTypes

android - GCMIntentService 的 onRegistered() 从未调用过

android - 跨多行显示两个 TextViews

android - 以编程方式生成的 TextView 的全息效果?

java - Android:可点击 TextView 中的 ClickableSpan