行のあるテーブルレイアウトがあり、各行にたくさんのテキストビューがあります (すべてプログラムで追加され、xml はありません)。行にマージンを取得しているので、セルではなく行にマージンを取得しています。また、セルで RelativeLaout.LayoutParams を使用しようとしましたが、それも機能しません。誰でも私の問題の解決策を提案できますか?
			
			1289 次
		
1 に答える
            1        
        
		
ビューには正しいものLayoutParamsを使用する必要があります。ビューを次の場所に追加するとき:
TableLayout; 使用するTableLayout.LayoutParamsTableRow; 使用してTableRow.LayoutParamsください。
編集:コードを編集しました。一部のセルに黒いスペースが必要です。そのビューに同じ背景色を設定することでこれを達成できます(またはセルの行とビューの透明な背景)。tableLayout で textViews に同じ背景色を設定する方が簡単だと思います。
以下は、試すことができる完全なコード サンプルです (出力を簡単に確認できるように色が追加されています)。
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TableLayout tableLayout = createTableLayout(16, 14);
    setContentView(tableLayout);
    makeCellEmpty(tableLayout, 1, 6);
    makeCellEmpty(tableLayout, 2, 3);
    makeCellEmpty(tableLayout, 3, 8);
    makeCellEmpty(tableLayout, 3, 2);
    makeCellEmpty(tableLayout, 4, 8);
    makeCellEmpty(tableLayout, 6, 9);
}
public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
    // get row from table with rowIndex
    TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
    // get cell from row with columnIndex
    TextView textView = (TextView)tableRow.getChildAt(columnIndex);
    // make it black
    textView.setBackgroundColor(Color.BLACK);
}
private TableLayout createTableLayout(int rowCount, int columnCount) {
    // 1) Create a tableLayout and its params
    TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setBackgroundColor(Color.BLACK);
    // 2) create tableRow params
    TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
    tableRowParams.setMargins(1, 1, 1, 1);
    tableRowParams.weight = 1;
    for (int i = 0; i < rowCount; i++) {
        // 3) create tableRow
        TableRow tableRow = new TableRow(this);
        tableRow.setBackgroundColor(Color.BLACK);
        for (int j= 0; j < columnCount; j++) {
            // 4) create textView
            TextView textView = new TextView(this);
            textView.setText(String.valueOf(j));
            textView.setBackgroundColor(Color.WHITE);
            textView.setGravity(Gravity.CENTER);
            // 5) add textView to tableRow
            tableRow.addView(textView, tableRowParams);
        }
        // 6) add tableRow to tableLayout
        tableLayout.addView(tableRow, tableLayoutParams);
    }
    return tableLayout;
}
そして、マージンが正しく適用されていることを確認できるこのコードの出力は次のとおりです。

于 2013-11-10T21:13:12.357   に答える