【问题标题】:Translate Custom Notification String翻译自定义通知字符串
【发布时间】:2021-04-13 17:31:32
【问题描述】:

我的应用支持 2 种语言,希伯来语和英语。我正在尝试使用 2 个操作按钮进行自定义通知,但通知描述是希伯来语而不是英语(我猜它使用我的手机默认系统语言),除了按钮,它们工作正常。

但是,当我不使用自定义通知时,一切正常(最后一盒代码)。

自定义通知代码:

NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_launch, getString(R.string.launch), activityPendingIntent);
NotificationCompat.Action action2 = new NotificationCompat.Action(R.drawable.ic_cancel, getString(R.string.stop), servicePendingIntent);


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
        .setContentTitle(getString(R.string.letherknow))
        .setContentText(getString(R.string.running_in_the_backgroud))
        .addAction(action)
        .addAction(action2)
        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)
        .setColorized(true)
        .setColor(getColor(R.color.foreGroundBackgroundColor))
        .setPriority(NotificationManager.IMPORTANCE_HIGH)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setSmallIcon(R.drawable.applogo)
        .build();

自定义通知 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="60dp">

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/title"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp"
        android:layout_toEndOf="@+id/image"
        android:text="@string/running_in_the_backgroud"
        android:textAlignment="center"
        android:textColor="#008066"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="0dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="9dp"
        android:layout_toEndOf="@+id/image"
        android:text="@string/title"
        android:textAlignment="center"
        android:textColor="#008066"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

正常的通知:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
        .setContentTitle(getString(R.string.letherknow))
        .addAction(action)
        .addAction(action2)
        .setContentText(getString(R.string.launch))
        .setColor(getColor(R.color.foreGroundBackgroundColor))
        .setPriority(NotificationManager.IMPORTANCE_HIGH)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.applogo))
        .setSmallIcon(R.drawable.applogo)
        .build();

谢谢!

编辑: 我还检查了 build.gradle 中的 resConfigs:

defaultConfig {
    resConfigs "en-rUS", "iw-rIL"
}

【问题讨论】:

  • string.launch 和 string.stop 是否在您的两个 xml 上正确翻译?
  • @javdromero 感谢您的回复,是的,他们正在工作,他们正在工作,只是描述有误

标签: java android locale android-notifications foreground-service


【解决方案1】:

解决方案:

所以经过长时间的研究,因为我使用 RemoteView 作为自定义通知,所以我使用了setTextViewText 并添加了以下内容:

mRemoteView.setTextViewText(R.id.customnotificationtext,getString(R.string.runninginbackground));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多