【问题标题】:Using string constants in deeplink URI with Jetpack Navigation使用 Jetpack Navigation 在深度链接 URI 中使用字符串常量
【发布时间】:2020-11-24 09:13:00
【问题描述】:

是否可以在深层链接 URI 中使用字符串常量?

目前我有几个链接,导致主机部分重复:

<deepLink app:uri="www.host.com/a" />
<deepLink app:uri="www.host.com/b" />
<deepLink app:uri="www.host.com/c" />
<deepLink app:uri="www.host.com/d" />

因此,我正在寻找一种以某种方式提取 www.host.com 的方法。

我一直天真地尝试将其提取到strings.xml

<string name="host">www.host.com</string>

然后:

<deepLink app:uri="{@string/host}/details/{id}" />

但这不起作用(Improper use of wildcards and/or placeholders in deeplink URI host 编译错误)

【问题讨论】:

  • 对于strings.xml 文件,我使用了这种方法:stackoverflow.com/a/39870268 也许你检查一下这是否适合你的情况。
  • @BrunoBieri 有趣的解决方案!解决了重复的问题。但是是否可以在 Java/Kotlin 代码中访问这些字符串?
  • 嗯,是的。您需要将 global 字符串作为单独的字符串条目提供,就像在链接的答案中一样,他有 app_name 字符串。所以他可以在strings.xml中使用全局变量appname,在代码中使用app_name变量。
  • 这解决了我的问题。请添加您的评论作为答案,以便我将其标记为已接受。
  • 能否也提供解决方案。

标签: android android-architecture-navigation android-navigation android-jetpack-navigation android-deep-link


【解决方案1】:

正如 cmets 对这个问题所提到的,OP 想要在深层链接 uri 中使用常量字符串进行多次使用。名为 Entity declaration 的 XML 功能允许这样做。

here也回答了一个类似目标的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2019-08-27
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    相关资源
    最近更新 更多