Android Relativelayout以编程方式

Android Relativelayout以编程方式,android,android-relativelayout,android-tablelayout,Android,Android Relativelayout,Android Tablelayout,基本上,我正在尝试向表中添加行,我需要以编程方式进行此操作 我可以让它添加我想要的内容,但不是我想要的方式 例如,我希望文本视图位于左侧,按钮edittext按钮位于右侧 基本上我希望它如下图所示: 另外,我不想只显示文本视图的宽度和宽度 不管怎么说,到目前为止我已经做到了: XML: 我试过搜索等,但还是不能让它正常工作。。任何帮助都将不胜感激 LayoutInflater inflater = getLayoutInflater(); TableRow row = (TableRow

基本上,我正在尝试向表中添加行,我需要以编程方式进行此操作

我可以让它添加我想要的内容,但不是我想要的方式

例如,我希望文本视图位于左侧,按钮edittext按钮位于右侧

基本上我希望它如下图所示:

另外,我不想只显示文本视图的宽度和宽度

不管怎么说,到目前为止我已经做到了:

XML:

我试过搜索等,但还是不能让它正常工作。。任何帮助都将不胜感激

 LayoutInflater inflater = getLayoutInflater();
 TableRow row = (TableRow) inflater.inflate(R.layout.table,
                _tablelayout, false);
 TextView textview = (TextView) row.findViewById(R.id.rowdata);
这样你就可以满足你的要求了。这足够吗

最好的
我觉得你把事情弄得太复杂了。如果我创建一个类似于链接图像的结构,我会使用一个列表视图,其中的行是从xml文件中膨胀出来的。这样一来,所有GUI内容都将在xml中定义,而您只需要在Java中执行一些细节?然后像这样?TableLayout table=(TableLayout)findviewbyd(R.id.tableLayout1);LayoutInflater充气机=getLayoutInflater();TableRow row=(TableRow)充气器。充气(R.layout.TableRow,table,false);在几次奇怪的结果之后,有点胡闹。。然而,在玩过之后,我想我把它恢复到原来的状态,现在它工作得很好了这让人困惑,但谢谢:)
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);

TableRow row = new TableRow(vanstock.this);

RelativeLayout RowLayout = new RelativeLayout(vanstock.this);
RowLayout.setId(99);

TextView lblItem = new TextView(vanstock.this);
lblItem.setId(1);
lblItem.setText("ddfsgsdfgs");

RelativeLayout.LayoutParams lblitemParam = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lblitemParam.addRule(RelativeLayout.CENTER_VERTICAL,
        RelativeLayout.TRUE);
lblitemParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
        RelativeLayout.TRUE);

lblItem.setLayoutParams(lblitemParam);
RowLayout.addView(lblItem);

Button btnPlus = new Button(vanstock.this);
btnPlus.setId(4);
btnPlus.setText("+");
btnPlus.setWidth(40);

RelativeLayout.LayoutParams btnPlusParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
        RelativeLayout.TRUE);
btnPlusParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,
        RelativeLayout.TRUE);
btnPlus.setLayoutParams(btnPlusParams);

RowLayout.addView(btnPlus);

EditText txtItem = new EditText(vanstock.this);
txtItem.setId(3);
txtItem.setWidth(40);

RelativeLayout.LayoutParams txtItemParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
txtItemParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,
        RelativeLayout.TRUE);
txtItemParams.addRule(RelativeLayout.LEFT_OF, btnPlus.getId());
txtItem.setLayoutParams(txtItemParams);

RowLayout.addView(txtItem);

Button btnMinus = new Button(vanstock.this);
btnMinus.setId(2);
btnMinus.setText("-");
btnMinus.setWidth(40);

RelativeLayout.LayoutParams btnMinusParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
btnMinusParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
btnMinusParams.addRule(RelativeLayout.LEFT_OF, txtItem.getId());
btnPlus.setLayoutParams(btnMinusParams);

RowLayout.addView(btnPlus);
row.addView(RowLayout);

table.addView(row, new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT));
 LayoutInflater inflater = getLayoutInflater();
 TableRow row = (TableRow) inflater.inflate(R.layout.table,
                _tablelayout, false);
 TextView textview = (TextView) row.findViewById(R.id.rowdata);