如何在 Android Jetpack Compose 中使用字符串资源? [英] How to use string resources in Android Jetpack Compose?

查看:107
本文介绍了如何在 Android Jetpack Compose 中使用字符串资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我有以下 strings.xml 资源文件:

Let's I have the following strings.xml resource file:

<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>

如何在 Jetpack Compose 中使用这些资源?

How can I use these resources in Jetpack Compose?

推荐答案

androidx.compose.ui.res 包包含加载字符串资源和其他资源类型的函数.

There's androidx.compose.ui.res package containing functions for loading string resources as well as other resource types.

您可以使用 stringResource() 函数,例如:

You can get a string using stringResource() function, for example:

...
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")
  )
}

字符串数组

可以使用 stringArrayResource 获得() 函数:

val planets = stringArrayResource(R.array.planets_array)

复数(数量字符串)

从 compose 1.0.0-alpha10 开始,没有用于获取复数资源的内置函数,但您可以通过 LocalContext 并像在基于视图的应用程序中一样使用它.如果您创建自己的类似于其他资源函数(例如 Jetcaster 撰写示例例如):

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. Even better if you create your own function similar to other resource functions (like Jetcaster compose sample does for example):

// 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. 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)
  )
}

这篇关于如何在 Android Jetpack Compose 中使用字符串资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆