问题描述
项目支持RTL;特别是阿拉伯语。 要求是所有数字都必须以英文格式出现,即使语言选择为阿拉伯语区域设置,我们在任何地方实现它的方式通常是 TextViews 的以下代码:
TextView t = findViewById(R.id.sampleTV);
t.setText(String.format("123",Locale.US));
这里 123 将显示为“123”,因为我们在美国语言环境中将其格式化为硬编码而不是“١٢٣”
我想为 Android Material 组件的底部导航提供的徽章通知实现相同的目标,这是我尝试过的:
if(navigation!=null) {
NumberFormat nf = NumberFormat.getInstance(Locale.US);
BadgeDrawable badgeDrawable = navigation.getorCreateBadge(R.id.nav_account);
if (badgeDrawable != null) {
if((counterNoti + counter + counterRoom) <= 0) {
badgeDrawable.setVisible(false);
badgeDrawable.clearNumber();
}
else
{
int x = counterNoti + counter + counterRoom;
nf.format(x);
badgeDrawable.setVisible(true);
badgeDrawable.setNumber(x);
}
}
(where navigation is reference to my BottomNavigation!)
然而,即使这样,数字仍以阿拉伯语显示,不像 setText 的情况。有没有办法实现这一点,或者我需要一个替代方案?
感谢您的投入和时间!谢谢。
解决方法
以下对我有用,通过在 onCreate 中调用以下函数来更改当前 JVM 实例的本地
private fun numbersFormat() {
val locale = Locale.ENGLISH
Locale.setDefault(locale)
}
,
您无法以简单的方式更改徽章数字文本区域设置。我的决定是通过 xml 创建自定义徽章:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_badge"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginTop="4dp"
android:layout_marginRight="0dp"
android:background="@drawable/bg_badge"
android:gravity="center"
android:minWidth="21dp"
android:minHeight="21dp"
android:textAppearance="@style/Typography.BadgeCounter"
android:textColor="#ffffff"
tools:ignore="RtlHardcoded"
tools:text="999+" />
在您的片段中扩展布局并设置 TextView 的文本如下:
txtCounter.text = NumberFormat.getInstance(Locale.ENGLISH).format(counter)