2012-12-04 35 views
1

可能重复:
Android dynamic String Resources是否可以在Android中使用Strings加载资源?

我有以下几点:

TextView text = (TextView)vi.findViewById(R.id.text); 
TextView txtSub = (TextView)vi.findViewById(R.id.txtSub); 
TextView txtA = (TextView)vi.findViewById(R.id.txtA); 
TextView txtB = (TextView)vi.findViewById(R.id.txtB); 

是否有可能访问这些使用字符串而不是(这只是伪代码)?

String resourceName = "text"; 
TextView text = (TextView)vi.findViewById(R.id. + resourceName); 

回答

2

使用Resources.getIdentifier()

int resource = getResources().getIdentifier("R.id."+resourceName, "string", getPackageName()); 
TextView text = (TextView)vi.findViewById(resource); 
相关问题