这个问题实际上与这篇文章有关.以编程方式设置TextView的布局权重
根据答案我只需要设置TextView布局参数如下并设置stretchColumn属性,但通过添加以下代码,它使textView从表格布局中消失.
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Run Code Online (Sandbox Code Playgroud)
所以,这是我的代码,我想动态添加一行3列,权重为35%,5%和60%.请告诉我我的代码有什么问题.
private void addTableRow(int resIdTitle, String strValue){
TableRow tr = new TableRow(this);
// Add First Column
TextView tvTitle = new TextView(this);
tvTitle.setText(resIdTitle);
tvTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.35f));
tr.addView(tvTitle);
// Add 2nd Column
TextView tvColon = new TextView(this);
tvColon.setText(" : ");
tvColon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.05f));
tr.addView(tvColon);
// Add the 3rd Column
TextView tvValue = new TextView(this);
tvValue.setText(strValue);
tvValue.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.60f));
tr.addView(tvValue);
// Finally add it to the table
tl.addView(tr, …Run Code Online (Sandbox Code Playgroud)