正从字符串布局ID,将无法正常工作 [英] getting layout id from string, won't work

查看:139
本文介绍了正从字符串布局ID,将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过每一个问题来看,不能让它的工作。

Looked through every question, can't get it working.

我想设置一个AppWidgets布局字符串的值(这样的布局可以通过只是改变了串切换到另一个)。

I wanna set an AppWidgets layout with the value of a string (So the layout can be switched to another one by just changing the string).

String NoteString = "R.layout.widget_blue".toString();

int resID = context.getResources().getIdentifier(NoteString, "layout", context.getPackageName());

RemoteViews views = new RemoteViews(context.getPackageName(), resID);

不知道为什么它不工作,只部件表示:问题加载部件。

don't know why it doesn't work, widget just says: "problem loading widget"..

这工作得很好:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_blue);

感谢

推荐答案

为了构建从字符串组件的资源ID,你必须通过单个部件到则getIdentifier(),不完全限定ID作为一个参数:

In order to construct a resource ID from string components, you must pass the individual components into getIdentifier(), not the fully qualified ID as a single parameter:

//Get the ID for R.layout.widget_blue
int resID = context.getResources().getIdentifier("widget_blue", "layout", context.getPackageName());

然后,只需更改第一个参数名称来获得不同的布局资源。

Then just change the first parameter name to get a different layout resource.

这篇关于正从字符串布局ID,将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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