【发布时间】:2023-03-26 22:51:01
【问题描述】:
我有一个巨大的活动布局,我使用setContentView() 设置。在那个布局中,我有一个我想要填充的TableLayout(在我的活动中命名为tableLayout)。该 TableLayout 的行是布局文件中的自定义视图(我们称该文件为tablerow_layout.xml)。在这个布局中,我有一些 TextViews 和 ImageView。我想要做的是在创建行时以编程方式更改 TextView 的内容。这是我的代码:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRow = layoutInflater.inflate(R.layout.tablerow_layout, null, false);
TextView name = (TextView) newRow. findViewById(R.id.tablerow_name);
name.setText("THIS LINE");
tableLayout.addView(newRow);
不幸的是,当我尝试更改 TextView 的内容时,我的应用程序崩溃了。我知道我可以使用 ListView 来做到这一点,但我通常只有少量的行,并且为 ListView 创建另一个适配器的开销非常大。
正如@ρяσѕρєя K 建议的那样,我也尝试了 newRow.findViewById(...) 而不是 findViewById(...) 没有任何区别
【问题讨论】:
标签: android android-tablelayout layout-inflater