我有一个表,它在主 xml 布局中进行了描述。我需要知道,是否有机会为每个表行设置布局,这样我就不需要每次都重新创建?如果布局很简单,我可以通过代码来完成,但此时布局相当复杂。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ebank_saskaitos);
this.setFirstBlock();
}
private void setFirstBlock() {
TableLayout ll = (TableLayout) findViewById(R.id.first_block);
ll.addView(this.tableChild());
}
private TableRow tableChild() {
TableRow tr = new TableRow(this);
//row implementation
return tr;
}
最佳答案
像这样使用LayoutInflater
:
private View tableChild() {
TableRow tr = new TableRow(this);
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_for_my_table_row, tr, false);
//want to get childs of row for example TextView, get it like this:
TextView tv = (TextView)v.findViewById(R.id.my_row_text_view);
tv.setText("This is another awesome way for dynamic custom layouts.");
return v;//have to return View child, so return made 'v'
}
关于java - Android:将 xml 布局设置为表行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10411239/