Fel*_*lix 5 android layout-inflater android-tablelayout
我为我的活动设置了一个巨大的布局,我使用setContentView(). 在该布局中,我有一个TableLayout(tableLayout在我的活动中命名)要填充。该 TableLayout 的行是布局文件中的 customViews (我们称之为该文件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);
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试更改 TextView 的内容时,我的应用程序崩溃了。我知道我可以使用 ListView 来做到这一点,但我通常只有少量行,并且为 ListView 创建另一个适配器的开销非常大。
作为 @???????K 建议我也尝试过 newRow.findViewById(...) 而不是 findViewById(...) 没有任何区别
如何从膨胀的布局中获取视图的参考
使用View从方法返回的对象LayoutInflater.inflate来访问作为第一个参数传递给inflate方法的布局中的视图。
在您的情况下,使用对象从布局newRow访问视图 :R.layout.tablerow_layout
TextView name = (TextView)newRow.findViewById(R.id.tablerow_name);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9427 次 |
| 最近记录: |