Android以编程方式将linearLayout添加到tableRow

Android以编程方式将linearLayout添加到tableRow,android,tablelayout,tablerow,Android,Tablelayout,Tablerow,我正在以编程方式填充tableLayout。我希望我的排成这样 <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/icon" android:i

我正在以编程方式填充tableLayout。我希望我的排成这样

<TableRow android:id="@+id/tableRow1"
                        android:layout_width="wrap_content" android:layout_height="wrap_content">
                        <ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
                            android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
                        <LinearLayout android:id="@+id/linearLayout2"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:orientation="vertical">
                            <TextView android:id="@+id/textView2" android:text="TextView"
                                android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
                            <TextView android:text="TextView" android:id="@+id/textView1"
                                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
                        </LinearLayout>
                        <Button android:text="Button" android:id="@+id/button1"
                            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                    </TableRow>
但linearlayout未显示在行中。我哪里出错了?? 谢谢

您可能会看到,它解释了如何以编程方式创建一个
表格行
,从部分
XML
定义中扩展它

编辑,评论后:链接答案的要点是:

  • 创建表示表格行的局部布局,并设置其自己的样式:

  • 通过以下代码动态充气部件:
TableRow tr=(TableRow)getLayoutInflater().充气(R.layout.partial\u table\u row,null);

以下是我如何以编程方式将TableRows添加到表布局中。每个视图问题实际上都是一个XML文件,独立于放置在其中的片段。该片段有一个Tablelayout,questionContainer,它接受每个新问题并将其添加到底部(questionContainer位于滚动视图中):

公共类EconFragment扩展了SherlockFragment{
专用表格和容器;
int pos=0;
私有字符串[]titles={“第一个标题”、“hallo1”、“hallo2”、“hallo3”,
“哈罗四号”、“哈罗五号”、“哈罗六号”、“哈罗七号”、“哈罗八号”、“哈罗九号”};
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
Log.v(“Econ”、“onCreateView”);
视图v=充气机。充气(R.layout.econfragment,容器,错误);
questionContainer=(TableLayout)v.findViewById(R.id.questionContainer);
//学士学位
int leftMargin=5;
int-topMargin=5;
int rightMargin=5;
int-bottomMargin=5;
而(位置<10){
视图问题=充气机。充气(R.layout.question,null);
问题.setId(pos);
TextView title=(TextView)question.findViewById(R.id.questionTextView);
title.setText(titles[pos]);
按钮图表=(按钮)问题.findViewById(R.id.chartsButton);
图表。setId(pos);
charts.setOnClickListener(chartsListener);
TableRow tr=(TableRow)问题;
TableLayout.LayoutParams trParams=新建TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_父级,
TableLayout.LayoutParams.WRAP_内容);
trParams.setMargins(左页边距、上页边距、右页边距、下页边距);
tr.setLayoutParams(trParams);
Log.v(“经济”、“while循环”);
questionContainer.addView(tr);
pos++;
}
pos=0;
返回v;
}
如果我知道需要添加多少布局,我会这样做。
TableRow TableRow;
tableRow=findViewById(R.id.tableRow1);
//添加2个线性布局

对于(int i=1;我使用层次结构查看器查看发生了什么,并请报告。您正在调用
main.setLayoutParams(…
),我将尝试
main.setLayoutParams(…
)。
TableRow row = new TableRow(activity);
      row.setGravity(Gravity.CENTER);
      row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));


      image.setPadding(10, 10, 10, 10);       
      ((LinearLayout) image).setGravity(Gravity.LEFT);
      row.addView(image);

      LinearLayout main = new LinearLayout(row.getContext());
     main.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
      main.setOrientation(LinearLayout.VERTICAL);


      TextView nameView = new TextView(activity);

      if(name.length() > 22)
          nameView.setText(name.substring(0, 22)+"...");
      else
          nameView.setText(name);

      nameView.setPadding(10, 10, 10, 10);
      nameView.setTextColor(Color.parseColor("#83be56"));
      nameView.setTypeface(null,Typeface.BOLD);
      nameView.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));

      main.addView(nameView,new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));

      row.addView(main);

      bt.setText("Respond");
      bt.setPadding(10,10,10,10);
      bt.setGravity(Gravity.CENTER);

      row.addView(bt);
public class EconFragment extends SherlockFragment {

private TableLayout questionContainer;
int pos = 0;
private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
        "hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.v("Econ", "onCreateView");
    View v = inflater.inflate(R.layout.econfragment, container, false);
    questionContainer = (TableLayout) v.findViewById(R.id.questionContainer);
    //bs
    int leftMargin=5;
    int topMargin=5;
    int rightMargin=5;
    int bottomMargin=5;
    while (pos < 10) {
    View question = inflater.inflate(R.layout.question, null);
    question.setId(pos);
    TextView title = (TextView) question.findViewById(R.id.questionTextView);
    title.setText(titles[pos]);
    Button charts = (Button) question.findViewById(R.id.chartsButton);
    charts.setId(pos);
    charts.setOnClickListener(chartsListener);
    TableRow tr = (TableRow) question;
    TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT);
    trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
    tr.setLayoutParams(trParams);
    Log.v("econ", "while loop");
    questionContainer.addView(tr);
    pos++;
    }
    pos = 0;
    return v;
}
I would do something like this if I know the how many layouts i need to add.

    TableRow tableRow ;

    tableRow = findViewById(R.id.tableRow1);

    //Adding 2 LinearLayout 
    for (int i = 1; i <= 2; i++) {
        LinearLayout linearLayout= new LinearLayout (this);
        tableRow.addView(linearLayout);
    }