Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layout

送分小仙女□ 提交于 2019-12-06 01:44:17

yawus this is a good tutorial will help you set the table layout http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

this is the xml layout

http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/

this is the activity code

http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/

TableRow tableRow= new TableRow(this);

            ArrayList<Object> row = data.get(position);

            TextView idText = new TextView(this);
            idText.setText(row.get(0).toString());
            tableRow.addView(idText);

            TextView textOne = new TextView(this);
            textOne.setText(row.get(1).toString());
            tableRow.addView(textOne);

            TextView textTwo = new TextView(this);
            textTwo.setText(row.get(2).toString());
            tableRow.addView(textTwo);

            dataTable.addView(tableRow);

You need to add a RelativeLayout to each Table

TableRow row = new TableRow(this);
                            row.setId(tag);
                         // Creating a new RelativeLayout

                            RelativeLayout relativeLayout = new RelativeLayout(this);
                            // as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
                            TableRow.LayoutParams rlp = new TableRow.LayoutParams(
                                    TableRow.LayoutParams.MATCH_PARENT,
                                    TableRow.LayoutParams.MATCH_PARENT);   
                            rlp.setMargins(0, 0, 0, 0);
                            row.addView(relativeLayout, rlp);

Then you add your TextView to the RelativeLayout. Like this

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.WRAP_CONTENT,
                          RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.WRAP_CONTENT,
                          RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);

This is the way I handle positioning interface components within a table row.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!