0

次のレイアウトをプログラムで複製しようとしています。ただし、setGravityとsetPaddingは効果がないようです。

TableRows内にテキストビューを再作成しようとしています(パディングがあり、2番目のテキストビューが右揃えになっていることに注意してください)。また、動的であるため、追加のエントリを再作成しようとしています。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/myinfo_root"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:scrollbars="vertical"
        >
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainTable"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">

    <TableRow>
        <TextView
            android:layout_column="1"
            android:text="Text1"
            android:padding="3dip" />

        <TextView
            android:id="@+id/text1"
            android:text="TBD"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_column="1"
            android:text="Text2"
            android:padding="3dip" />

        <TextView
            android:id="@+id/text2"
            android:text="TBD"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
    . . .

TextViewsを追加するためのコードスニペットを次に示します。動作しているように見えますが、エントリにパディングがなく、正しく配置されていません。

    int n = 0;
    TableLayout table = (TableLayout) findViewById(R.id.mainTable);

    for (int j=0; j<2; j++) {
        Log.v(LOG_TAG, "entering for loop");
        n++;

        TableRow tr = new TableRow(getApplicationContext());
        tr.setId(100+n);

        String key = "hello";
        float value = (float) 1.388);
        value = value/3600;

        TextView tvDynamicKey = new TextView(getApplicationContext());                      
        TextView tvDynamicUnit = new TextView(getApplicationContext());

        tvDynamicUnit.setText(Float.toString(value));
        tvDynamicUnit.setGravity(Gravity.RIGHT);
        tvDynamicKey.setText(key + " " + n); 
        tvDynamicKey.setPadding(3, 3, 3, 3);
        tvDynamicUnit.setPadding(3, 3, 3, 3);
        tr.addView(tvDynamicKey);
        tr.addView(tvDynamicUnit);
        table.addView(tr);
    }    
  }
4

1 に答える 1

1

レイアウトでは、常に左側の要素にcolumn1を指定します(android:layout_column = "1")。しかし、動的な作成のためにプログラムでそれを行うことはありません。あなたはこのようにそれを行うことができます。私はそれをテストしました、それは私の環境で動作します。

 tvDynamicKey.setLayoutParams(new TableRow.LayoutParams(1));
于 2011-11-03T22:07:15.987 に答える