3

TableRowsをTableLayoutに追加しようとしていますが、これは素晴らしく機能しています。ただし、レイアウトパラメータでいくつかの問題が発生しています。TableRow内のTextView間の間隔は、思ったように機能していません。

私の現在のコードは次のようになります。

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とそれに含まれるTextViewのそれぞれに、既存のTableRowとTextViewのレイアウトプロパティを継承させたいと思います。

4

2 に答える 2

1

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);
于 2012-03-03T04:42:39.460 に答える
1

各テーブルに 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);

これは、表の行内にインターフェイス コンポーネントを配置する方法です。

于 2013-05-27T11:29:37.853 に答える