【问题标题】:How to access an Android color resource from JS in a NativeScript project?如何在 NativeScript 项目中从 JS 访问 Android 颜色资源?
【发布时间】:2019-06-26 04:14:51
【问题描述】:

在我的 NativeScript 项目中,我在 App_Resources/Android/src/main/res/values/colors.xml 中定义了一种颜色:

<resources>
    ...
    <color name="notification">#00546A</color>
</resources>

现在我尝试像这样访问定义颜色的值:

utils.ad.getApplicationContext().getResources().getColor(
    android.R.color.notification, 
    null
)

但我总是得到:

android.content.res.Resources&NotFoundException:资源 ID #0x0

我也试过了:

android.support.v4.content.ContextCompat.getColor(
    utils.ad.getApplicationContext(), 
    android.R.color.notification
)

...但结果相同。

有人有提示吗?

【问题讨论】:

    标签: android colors resources nativescript


    【解决方案1】:

    试试这个

    var app = require('tns-core-modules/application');
    app.getNativeApplication().getResources().getIdentifier("color_name", "color", app.getNativeApplication().getPackageName());
    

    【讨论】:

    • 将颜色 ID 转换为 NativeScript 颜色对象:new Color(app.getNativeApplication().getResources().getColor(colorId));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多