添加两个布局参数到addView()

时间:2015-12-09 20:30:16

标签: android android-layout

如何为addView()添加两个布局参数?例如:我为行创建了第一个TableRow.LayoutParams白色边距,为span创建了第二个TableRow.LayoutParams。然后我想将这两个参数添加到addView()

这是我的代码:

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TableLayout tableLayout = new TableLayout(this);

        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
        TableRow.LayoutParams tableRowParams2= new TableRow.LayoutParams();

        tableRowParams.setMargins(1, 1, 1, 1);
        tableRowParams2.span = 2;


        TableRow row1 = new TableRow(this);
        TableRow row2 = new TableRow(this);



        TextView text1 = new TextView(this);
        text1.setText("text1");
        text1.setBackgroundColor(Color.GRAY);

        TextView text2 = new TextView(this);
        text2.setText("text2");
        text2.setBackgroundColor(Color.GRAY);

        TextView text3 = new TextView(this);
        text3.setText("text3");
        text3.setBackgroundColor(Color.GRAY);

        TextView text4 = new TextView(this);
        text4.setText("text4");
        text4.setBackgroundColor(Color.GRAY);

        TextView text5 = new TextView(this);
        text5.setText("text5");
        text5.setBackgroundColor(Color.GRAY);

        TextView text6 = new TextView(this);
        text6.setText("text6");
        text6.setBackgroundColor(Color.GRAY);

        row1.addView(text1, tableRowParams);
        row1.addView(text2, tableRowParams2);

        row2.addView(text4, tableRowParams);
        row2.addView(text5, tableRowParams);
        row2.addView(text6, tableRowParams);

        tableLayout.addView(row1);
        tableLayout.addView(row2);

        setContentView(tableLayout);

    }

}

如何为row1.addView()?

添加两个参数

3 个答案:

答案 0 :(得分:0)

也许这会有所帮助!

How can i add more than 1 Views in one cell in TableRow?

您需要将它们放在某种容器中,例如LinearLayout或RelativeLayout。每个单元格只能有一个视图,但如果该视图是一个容器,那么一个视图实际上可以由多个视图组成。

答案 1 :(得分:0)

为什么不用两个配置定义LayoutParams对象?

TableRow.LayoutParams tableRowParamsExample = new TableRow.LayoutParams();
tableRowParamsExample .setMargins(1, 1, 1, 1);
tableRowParamsExample .span = 2;

row1.addView(text1, tableRowParamsExample);

然而,这是一种解决方法。您还可以编辑要更改的行的特定LayoutParams。例如:

TableRow row1 = new TableRow(this);
TableRow.LayoutParams newParams = buildParams(row1);
row1.setLayoutParams(newParams);

并将逻辑提取到方法,例如

private LayoutParams buildParams(TableRow row) {
    TableRow.LayoutParams newParams = new TableRow.LayoutParams(row.getLayoutParams());
    newParams.span = 12;
    return newParams
}

答案 2 :(得分:0)

Rafael Cardoso,你为我提出了一个想法。我用这种方式解决了我的问题:

TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
TableRow.LayoutParams tableRowParams2 = new TableRow.LayoutParams();

tableRowParams.setMargins(1, 1, 1, 1);

tableRowParams2.span = 2;
tableRowParams2.setMargins(1, 1, 1, 1);

现在我可以将tableRowParams2应用于我想要的任何单元格。