2012-12-04 33 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); 
相關問題