【发布时间】:2021-05-26 03:23:43
【问题描述】:
在 android 上通过使用动态字符串作为 id 来获取带有 layoutInflater 的 View 时遇到问题。
此代码有效:
LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newView = layoutInflater.inflate(R.layout.my_layout, null);
scrollViewMain.removeAllViews();
scrollViewMain.addView(newView);
但我想通过使用这样的字符串来动态获取视图:
String myString = "my_layout";
LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newView = layoutInflater.inflate(R.layout.myString, null);
scrollViewMain.removeAllViews();
scrollViewMain.addView(newView);
感谢您帮助理解这一点。
【问题讨论】:
标签: java android string layout