如何在android中使用数据绑定显示类似(5%off)的文本?

发布于 2025-01-16 15:25:15 字数 725 浏览 5 评论 0原文

我需要使用数据绑定在文本视图中显示文本(5% 折扣)。我的代码是

<com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_5"
            android:text="@{@string/percentage_off(priceUIModel.discountOfferPercentage)}"
            android:textAppearance="@style/Heading2Lines.Size12.Light"
            android:textColor="@color/red_100"
            app:visibility="@{priceUIModel.discountOfferPercentage !=null}" />

在 string.xml 中

<string name="percentage_off">%s\%% Off</string>

只需要在文本的前面和后面添加括号。我怎样才能修改这个string.xml?提前致谢。

I need to display text in text view (5% Off) using databinding. My code is

<com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_5"
            android:text="@{@string/percentage_off(priceUIModel.discountOfferPercentage)}"
            android:textAppearance="@style/Heading2Lines.Size12.Light"
            android:textColor="@color/red_100"
            app:visibility="@{priceUIModel.discountOfferPercentage !=null}" />

In string.xml

<string name="percentage_off">%s\%% Off</string>

Simply need to add brackets front and back of the text. How can I modify this string.xml? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2025-01-23 15:25:15

使用String.format()命令:

android:text='@{String.format("(%s)",@string/percentage_off(priceUIModel.discountOfferPercentage))}'

Use the String.format() command:

android:text='@{String.format("(%s)",@string/percentage_off(priceUIModel.discountOfferPercentage))}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文