Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从值中获取Resource.Id?_Android_Xamarin.android - Fatal编程技术网

Android 如何从值中获取Resource.Id?

Android 如何从值中获取Resource.Id?,android,xamarin.android,Android,Xamarin.android,在我的Drawable mdpi文件夹中有一个名为(my_image.png)的图像 我的android应用程序与Web服务交互。它可能会发回“我的图像”。我的android应用程序应该加载此图像 我用的是单机器人,我试过这个 int abc = Resources.GetIdentifier("my_image","drawable",null); 然而结果总是“0”。应该在什么时候(从资源文件) 环顾四周,android的方式似乎很相似 int i = this.getResourc

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

我的android应用程序与Web服务交互。它可能会发回“我的图像”。我的android应用程序应该加载此图像

我用的是单机器人,我试过这个

   int abc = Resources.GetIdentifier("my_image","drawable",null);
然而结果总是
“0”
。应该在什么时候(从资源文件)

环顾四周,android的方式似乎很相似

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

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

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

而不是:

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

因此,对于
资源
包“com.example”中的“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(资源名称,“drawable”,getPackageName());

对于字符串资源,我如下所示:

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()));

问题有两方面:

  • 您需要在
    Resources.GetIdentifier()
    调用中提供包名,而不要使用
    null
  • 您需要使用正确的包名
  • 确保获得正确包名的简单方法是使用以下属性:

    int id = Resources.GetIdentifier ("my_image", "drawable", PackageName);
    
    如果您不想/不能使用
    Context.PackageName
    ,请查看生成输出,例如
    obj\Debug\android\AndroidManifest.xml
    文件,并使用
    /manifest/@package
    属性值的值。例如,
    AndroidManifest.xml
    可能包含:

    
    
    因此,您可以使用:

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

    对于
    monodroid@lists.ximian.com
    subscribers,查看线程和。

    不,它似乎没有做任何事情。仍然得到零打印。我刚刚在自己的代码中测试了这一点,它工作了,所以让我们确保我们以相同的方式配置它。您能用您现在尝试的行更新您的问题吗?确保您已经完成了o不要使用“Resources.getSystem().getIdentifier(name,defType,defPackage)”,而是使用当前上下文中的Resources对象。我相信我已经找到了上一个示例行不起作用的原因。
    strings
    应该是
    string
    我发现像您一样使用包名(“MonoAndroidApplication2.MonoAndroidApplication2”)不起作用。我必须将其更改为两个不同的子字符串,例如:“MonoForAndroid.monoandroid应用程序2”如果我需要访问类似
    com.android.internal.R.string.ime\u action\u done
    int id = Resources.GetIdentifier ("my_image", "drawable", PackageName);
    
    int id = Resources.GetIdentifier ("my_image", "drawable", 
            "MonoAndroidApplication2.MonoAndroidApplication2");