【问题标题】:How to get the Resource.Id from Value?如何从 Value 中获取 Resource.Id?
【发布时间】:2011-08-11 17:39:27
【问题描述】:

我的 Drawable-mdpi 文件夹中有一个名为 (my_image.png) 的图像。

我的 android 应用程序与 web 服务交互。它可能会发回“my_image”。我的 android 应用程序应该会加载这张图片。

我正在使用 Monodroid,我试过了

   int abc = Resources.GetIdentifier("my_image","drawable",null);

然而结果总是"0"。什么时候应该(从资源文件)

        // aapt resource value: 0x7f020000
        public const int my_image = 2130837504;

环顾四周,android的方式好像也差不多

int i = this.getResources().getIdentifier("txt_asecondtext", "strings", this.getPackageName());

我尝试传入包名而不是 null,但没有任何效果。

【问题讨论】:

  • 我相信我明白了为什么您的最后一个示例行不起作用。 strings 应该是 string

标签: android xamarin.android


【解决方案1】:

问题是双重的:

  1. 您需要在Resources.GetIdentifier() 调用中提供包名称,而不是使用null
  2. 您需要使用正确的包名。

确保获得正确包名的简单方法是使用Android.Content.Context.PackageName 属性:

int id = Resources.GetIdentifier ("my_image", "drawable", PackageName);

如果您不想/不能使用Context.PackageName,请查看构建输出,例如obj\Debug\android\AndroidManifest.xml 文件,并使用 /manifest/@package 属性值的值。例如,AndroidManifest.xml 可能包含:

<?xml version="1.0" encoding="utf-8"?>
<manifest
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:versionCode="1"
        android:versionName="1.0"
        package="MonoAndroidApplication2.MonoAndroidApplication2">
    <!-- ... -->
</manifest>

因此,您可以改用:

int id = Resources.GetIdentifier ("my_image", "drawable", 
        "MonoAndroidApplication2.MonoAndroidApplication2");

对于monodroid@lists.ximian.com 订阅者,请参阅 How to use Resources.GetIdentifier() 线程和followup message

【讨论】:

  • 我发现像你一样使用包名(“MonoAndroidApplication2.MonoAndroidApplication2”)不起作用。我不得不将它更改为两个 2 个不同的子字符串,例如:“MonoForAndroid.MonoAndroidApplication2”
  • 如果我需要访问 com.android.internal.R.string.ime_action_done 之类的内容怎么办?
【解决方案2】:

您只需要正确格式化您的请求:

代替:

int i = this.getResources().getIdentifier("txt_asecondtext", "strings", this.getPackageName());

试试:

int i = getResources().getIdentifier("[fully qualified package]:drawable/[resource name]", null, null);

所以对于包“com.example”中的Resource“my_image”,它看起来像:

int i = getResources().getIdentifier("com.example:drawable/my_image", null, null);

更新:我还测试了我的以下工作(包括证明它的日志行:

int i = getResources().getIdentifier(
    getPackageName() + ":drawable/" + resource_name, null, null);
Log.d("EXAMPLE", "Just making sure that these IDs match: '" + i + "' == '" + R.drawable.resource_name + "'.");

这也可以像你上面那样格式化,我相信我已经指出了你的错误: int i = getResources().getIdentified(resource_name, "drawable", getPackageName());

【讨论】:

  • 不,这似乎没有做任何事情。仍然打印出零。
  • 我刚刚在我自己的代码中测试了它并且它工作正常,所以让我们确保我们以相同的方式配置它。你能用你现在尝试的那一行来更新你的问题吗?
  • 确保不使用“Resources.getSystem().getIdentifier(name, defType, defPackage)”,而是使用当前上下文中的资源对象。
【解决方案3】:

对于字符串资源,我这样做:

String s = "nameToFetch";
String text = getString(getResources().getIdentifier("str_" + s, "string", getPackageName()));

所以我认为你应该调用你的drawable:

String s = "nameToFetch";
Drawable drawable = getDrawable(getResources().getIdentifier("d_" + s, "drawable", getPackageName()));

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多