如何使用Android动态添加每个TableRow的按钮?

时间:2013-11-20 05:57:10

标签: java android xml tablelayout tablerow

我知道这个简单的问题,但我是新手.....我想为每一行添加按钮,例如对于row1应该有button1而对于row2应该有button2,按钮插入和文本使用for循环控制 的 first.xml

<LinearLayout 
     android:id="@+id/summaryHeaderLinear_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TableLayout 
        android:id="@+id/summaryTable_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*">
    </TableLayout>

firstrow.xml

 <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/summary_tablerow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout 
   android:id="@+id/summaryHeader_linearlayout"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
   <LinearLayout 
   android:id="@+id/summaryHeader_firstlinearlayout"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
    </LinearLayout>
    <LinearLayout 
   android:id="@+id/summaryHeader_secondlinearlayout"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
    </LinearLayout>
    <LinearLayout 
   android:id="@+id/summaryHeader_thirdlinearlayout"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">
  </LinearLayout>
  </LinearLayout>
</TableRow>

firstRow.java:

     setContentView(R.layout.first);
     summaryTableLayout = (TableLayout)findViewById(R.id.summaryTable_layout);
     summaryInflator = getLayoutInflater();
     for(int i =0;i<2;i++)
    {   final TableRow summaryTableRow = (TableRow)summaryInflator.inflate(R.layout.firstrow,null);
LinearLayout summaryHeaderLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_linearlayout);
summaryTableRow.setId(i);
Button summaryBtn = new Button(this);
summaryBtn.setId(i);
summaryBtn.setText("sample button:" +i);
summaryBtn.setBackgroundColor(Color.MAGENTA);
summaryTableRow.addView(summaryBtn);
 for(int k=0;k<5;k++)
{
LinearLayout summaryFirstHorizontalLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_firstlinearlayout);
TextView newText = new TextView(this);
newText.setId(k);
newText.setText(topRowArray[k]);
newText.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,1f));
newText.setBackgroundColor(Color.BLACK);
newText.setTextColor(Color.WHITE);
summaryFirstHorizontalLinearLayout.addView(newText);
 }
summaryTableLayout.addView(summaryTableRow);
} 

代码的输出是:    code snaphot
虽然我想创建此布局:

 Button for row1
      name qty cost sell margin
  Button for row2
     name qty cost sell margin

但它没有这样显示我已经定义了单独的xml我想要创建动态Row.I想要使用已定义的firstrow.xml请帮助我在现有代码中需要做些什么更改

1 个答案:

答案 0 :(得分:0)

用于在linearlayout上动态添加textview和按钮

    LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
    for(int y =0;y<5;y++){
        TextView textv= new TextView(this);
        final Button myButton = new Button(this);

        textv.setText("record");
        myButton.setText("button"+y);
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(textv,lp);
        ll.addView(myButton,lp);
}

我认为这对您的问题解决方案很有用