Android - динамическое добавление TableRow в TableLayout с использованием существующего макета TableRow - PullRequest
3 голосов
/ 03 марта 2012

Я пытаюсь добавить TableRows в TableLayout, который работает фантастически.Тем не менее, я сталкиваюсь с некоторыми проблемами с параметрами макета.Интервал между TextViews в TableRow не работает так, как я думал.

Мой текущий код выглядит следующим образом.

paymentTable = (TableLayout) findViewById(R.id.paymentTable);

for(i = 0; i < cursor.getCount(); i++) {

        TableRow row = new TableRow(this);

        TextView payAmount = new TextView(this);
        payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
        payAmount.setPadding(payAmount.getPaddingLeft(), 
                             payAmount.getPaddingTop(), 
                             textView.getPaddingRight(), 
                             payAmount.getPaddingBottom());

        TextView payDate = new TextView(this);
        payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));

        row.addView(payDate);
        row.addView(payAmount);

        paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        cursor.moveToNext();
    }

После этого TableLayout выглядит примерно так:из:

January$500

February$500

March$405

Но я хочу, чтобы это выглядело так:

January    $500
February   $500
March      $405

Чтобы прояснить ситуацию, я хочу, чтобы каждый новый TableRow и содержащиеся в нем TextViews наследовали свойства макета существующего TableRow.и TextView (s).

Ответы [ 2 ]

1 голос
/ 27 мая 2013

Вам необходимо добавить RelativeLayout к каждой таблице

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);

Затем вы добавляете свой TextView в RelativeLayout. Как это

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);

Это способ обработки компонентов интерфейса в строке таблицы.

1 голос
/ 03 марта 2012

yawus, это хороший учебник, который поможет вам установить макет таблицы http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

это макет XML

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

это код активности

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);
...