简体   繁体   English

Android Jetpack Compose 中如何使用字符串资源?

[英]How to use string resources in Android Jetpack Compose?

Let's I have the following strings.xml resource file:让我有以下strings.xml资源文件:

<resources>
    <!-- basic string -->
    <string name="app_name">My Playground</string>

    <!-- basic string with an argument -->
    <string name="greeting">Hello %!</string>

    <!-- plural string -->
    <plurals name="likes">
        <item quantity="one">%1d like</item>
        <item quantity="other">%1d likes</item>
    </plurals>

    <!-- string array -->
    <string-array name="planets">
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

How can I use these resources in Jetpack Compose?如何在 Jetpack Compose 中使用这些资源?

There's androidx.compose.ui.res package containing functions for loading string resources as well as other resource types.androidx.compose.ui.res package 包含用于加载字符串资源以及其他资源类型的函数。

string细绳

You can get a string using stringResource() function, for example:您可以使用stringResource() function 获取字符串,例如:

...
import androidx.compose.ui.res.stringResource

@Composable
fun StringResourceExample() {
  Text(
    // a string without arguments
    text = stringResource(R.string.app_name)
  )

  Text(
    // a string with arguments
    text = stringResource(R.string.greeting, "World")
  )
}

string array字符串数组

Can be obtained using stringArrayResource() function:可以使用stringArrayResource() function 获得:

val planets = stringArrayResource(R.array.planets_array)

plural (quantity string)复数(数量字符串)

As of compose 1.0.0-alpha10 there's no build-in function for obtaining a plural resource, but you can get the android context via LocalContext and use it the same way as you do it in a view-based app.从 compose 1.0.0-alpha10 ,没有用于获取复数资源的内置 function,但您可以通过LocalContext获取 android 上下文,并以与在基于视图的应用程序中相同的方式使用它。 Even better if you create your own function similar to other resource functions (like Jetcaster compose sample does for example):如果您创建自己的 function 类似于其他资源功能(例如Jetcaster compose sample),那就更好了:

// PluralResources.kt

package com.myapp.util

import androidx.annotation.PluralsRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext

@Composable
fun quantityStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): String {
    return LocalContext.current.resources.getQuantityString(id, quantity, *formatArgs)
}

so you can use it the same way:所以你可以以同样的方式使用它:

val likes = quantityStringResource(R.plurals.likes, 10, 10)

a note about recomposition关于重组的说明

As you know, in compose a composable function might be re-executed up to hundreds of times per second during recomposition .如您所知,在 compose 中,可组合的 function 在重组期间可能每秒重新执行数百次。 If the string you build isn't trivial and requires some calculation, it's better to remember the calculation result, so it won't be re-executed on each recomposition.如果您构建的字符串不是微不足道的并且需要进行一些计算,那么最好记住计算结果,这样就不会在每次重组时重新执行它。 For example:例如:

...

import androidx.compose.runtime.remember

@Composable
fun BirthdayDateComposable(username: String, dateMillis: Long) {
  // formatDate() function will be executed only if dateMillis value 
  // is changed, otherwise the remembered value will be used
  val dateStr = remember(dateMillis) {
    formatDate(dateMillis)
  }

  Text(
    text = stringResource(R.string.birthday_date, dateStr)
  )
}

This is now supported as of compose 1.2.0-alpha06 :从 compose 1.2.0-alpha06支持:

val likes = pluralStringResource(R.plurals.likes, 10, 10)

Here is an example;这是一个例子;

Text(text = stringResource(id = R.string.myString)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM