2

我有表,在主 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;
}
4

1 回答 1

5

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'
}
于 2012-05-02T09:50:18.060 回答