Android:将xml布局设置为表格行?

Pau*_*kis 2 java android tableview xml-layout

我有一张表格,在主要的 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;
}
Run Code Online (Sandbox Code Playgroud)

Adi*_*mro 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'
}
Run Code Online (Sandbox Code Playgroud)