在我们的应用程序中,我们创建了带有 id 的字符串,例如 msg_55164 、 msg_55165 ...... 现在在我们的应用程序中,在运行时我们从服务器获取 ID,例如 55164,并且根据该 ID,我们需要显示 ID 为 msg_55164 的字符串。是否可以在 Android 中执行类似的操作。
提前致谢! 帮助赞赏。 不山
最佳答案
您可以执行类似这样的操作来动态获取和加载 Drawable
:
String drawablePath = "msg_" + id; //Get here the drawable identifier name
int drawableID = getResources().getIdentifier(drawablePath,"drawable", getPackageName()); //Get the drawable ID
Drawable draw = getResources().getDrawable(drawableID);
或者这是一个简单的字符串:
String stringPath = "msg_" + id;
int stringID = getResources().getIdentifier(stringPath,"string", getPackageName());
String str = getResources().getString(stringID);
关于Android:需要从 strings.xml 中获取字符串,需要在运行时选择 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16748414/