在Android中以编程方式将LinearLayout添加到TableRow

3

我正在以编程方式填充一个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>

我已经尝试过使用代码来实现这个

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);

但是LinearLayout没有出现在行中。我错在哪里了?谢谢。

请使用层次结构查看器来查看发生了什么,并将其报告回来。 - Nanne
1
你正在调用 main.setLayoutParams(new LayoutParams(...,我建议尝试使用 main.setLayoutParams(new TableRow.LayoutParams(... - bigstones
3个回答

1
你可以看一下这个SO答案,它解释了如何通过编程创建一个TableRow,并从部分XML定义中填充它。 编辑,在评论后:链接答案的要点是:
  • 创建一个代表表行的部分布局,并设置其自己的样式:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"
  style="@style/tr_withborder" />
  • 通过代码动态填充部分:
TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.partial_table_row, null);

0
这是我如何通过编程向表格布局中添加TableRows的方法。每个View问题实际上都是一个独立于其所放置的片段的XML文件。该片段具有一个Tablelayout,questionContainer,它将每个新问题添加到底部(questionContainer位于scrollview中):
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;
}

0
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);
    } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接