【问题标题】:How can I make Notification language the same as phone language如何使通知语言与电话语言相同
【发布时间】:2016-09-18 20:37:18
【问题描述】:

我需要将此代码语言设置为与设备相同。

Notification.Builder builder = new Notification.Builder(this);
builder.SetContentTitle("ok");
builder.SetContentText("ok");
Notification nottt = builder.Build();
NotificationManager noeman = GetSystemService(Context.NotificationService) as NotificationManager;
noeman.Notify (122, nottt);
StopSelfResult(this, OnStartCommand);

【问题讨论】:

  • 请@joba 在下面将我的答案标记为正确。非常感谢。

标签: visual-studio xamarin.android


【解决方案1】:

这是因为您对通知中出现的文本进行了硬编码。

Android 在/res/values/strings.xml 中有一个名为strings.xml 的资源文件。

假设您的应用程序的默认语言是英语。还假设您要将应用程序中的所有文本本地化为葡萄牙语。 在这种情况下,您可以创建两个替代的 strings.xml 文件,每个文件都存储在特定于语言环境的资源目录中:

目录:

res/values/strings.xml

文件:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string
            name="string_name">nome</string>
    </resources>

包含应用程序使用的所有字符串的英文(本例中为默认)文本。

目录:

res/values-pt/strings.xml

文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string
        name="string_name">nome</string>
</resources>

包含所有字符串的葡萄牙语文本。

创建字符串文件后,您应该使用以下资源构建通知:

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(getString(R.string.string_name))
        .setContentText(getString(R.string.other_string));

更多信息请点击here

【讨论】:

    【解决方案2】:

    我认为您应该在字符串“ok”上按“alt+enter”使用(字符串资源)并选择(提取字符串资源), 然后使用 (getApplicationContext())

    builder.SetContentTitle(getApplicationContext().getString(R.string.ok));
    builder.SetContentText(getApplicationContext().getString(R.string.ok));
    

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 2011-03-04
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多