2012-01-09 63 views
0

在我的應用程序中,我想創建TableView Dynamicaly: 在這裏,我有xml視圖,就像我想在andriod中使用Dynamicaly一樣。Android:如何創建tableView Dynamicaly,就像下面的xml代碼一樣?

XML查看:

    <TableRow android:weightSum="1"> 
         <TextView android:gravity="left" 
         android:textStyle="bold" android:textSize="15sp" android:textColor="#EDA700" 
         android:layout_marginTop="3dp" android:layout_marginBottom="8dp" 
         android:text="PAYE calculations: Tax period 01 April 2011 to 31 March 2012" android:layout_weight="1"/> 
       </TableRow> 

        <View android:layout_height="2dip" android:background="#FF000000" /> 

        <TableRow android:weightSum="1"> 
         <TextView android:gravity="center" 
         android:textStyle="bold" android:textSize="16sp" android:textColor="#000000" 
         android:layout_marginTop="3dp" android:layout_marginBottom="3dp" 
         android:text="All employees: Total PAYE to withhold" android:layout_weight="1"/> 
        </TableRow> 

        <!-- taxcode value --> 
        <!-- =================== --> 
        <TableRow android:weightSum="1" android:layout_marginRight="5dp"> 
         <TextView android:gravity="left" 
         android:textSize="13sp" android:textColor="#000000" 
         android:layout_marginTop="3dp" android:layout_marginBottom="3dp" 
         android:text="Total gross income (salary/wages)" android:layout_weight="1"/> 
        <TextView android:gravity="center" 
          android:textSize="13sp" android:singleLine="true" 
         android:layout_marginLeft="5dp" android:layout_marginRight="5dp" 
         android:layout_marginTop="3dp" android:layout_marginBottom="3dp" 
         android:textColor="#000000" android:layout_weight="1"/> 
        <TextView android:id="@+id/taxcodeTextView" android:gravity="center" android:textColor="#000000" 
          android:textSize="13sp" android:singleLine="true" 
         android:layout_marginTop="3dp" android:layout_marginBottom="3dp" 
         android:layout_marginLeft="5dp" android:layout_marginRight="5dp" 
         android:text="00" /> 
        </TableRow>  

        <!-- KiwiSaver employee Deduction percentage--> 
        <!-- ====================================== --> 
        <TableRow android:weightSum="1" android:layout_marginRight="5dp"> 
         <TextView android:gravity="left" 
         android:textSize="13sp" android:textColor="#000000" 
         android:layout_marginTop="3dp" android:layout_marginBottom="3dp" 
         android:text="Total PAYE (including earner's levy)" android:layout_weight="1"/> 
        <TextView android:gravity="center" 
          android:textSize="13sp" android:singleLine="true" android:textStyle="bold" 
         android:layout_marginLeft="5dp" android:layout_marginRight="5dp" 
         android:layout_marginTop="3dp" android:layout_marginBottom="3dp" 
         android:text="minus" android:textColor="#000000" android:layout_weight="1"/> 
        <TextView 
         android:id="@+id/kiwiSaverEmployeeDeductionPercentageTextView" 
         android:gravity="center" android:textColor="#000000" 
          android:textSize="13sp" android:singleLine="true" 
         android:layout_marginTop="3dp" android:layout_marginBottom="3dp" 
         android:layout_marginLeft="5dp" android:layout_marginRight="5dp" 
         android:text="0.0%"/> 
        </TableRow>     
        </TableLayout> 

我已經創建dynamicaly視圖像下面的代碼:

List<TextView> textListWord = new ArrayList<TextView>(c2.getCount()); 
    List<TextView> textListAnswer = new ArrayList<TextView>(c2.getCount()); 
    List<ImageView> imageListAnswer = new ArrayList<ImageView>(c2.getCount()); 

    //for(int i = 0; i < c.getCount(); i++) 
    c2.moveToFirst(); 
    do 
    {  
     RelativeLayout innerLayout = new RelativeLayout(this); 
     innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     innerLayout.setBackgroundColor(0x00000000); 

     // set the Multiple TextView 
     TextView qWordTV = new TextView(getApplicationContext()); 
     TextView aWordTV = new TextView(getApplicationContext()); 
     ImageView aImageView = new ImageView(getApplicationContext()); 

     qWordTV.setText("\n"+c2.getString(3).toString()); 
     qWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     qWordTV.setTextColor(0xFFFF0000); 
     //qWordTV.layout(22, 0, 0, 0); 
     qWordTV.setPadding(22, 0, 0, 0); 

     aWordTV.setText("\n"+c2.getString(2).toString()); 
     aWordTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     aWordTV.setGravity(Gravity.CENTER); 
     aWordTV.setTextColor(0xFFFF0000);  
     if(c2.getString(2).equals(c2.getString(3))) 
     { 
      aImageView.setImageResource(R.drawable.correct); 
     } 
     else 
     { 
      aImageView.setImageResource(R.drawable.incorrect); 
     } 

     aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

     aImageView.setPadding(400, 25, 0, 0); 


     /**** Any other text view setup code ****/  

     innerLayout.addView(qWordTV); 
     innerLayout.addView(aWordTV); 
     innerLayout.addView(aImageView); 

     myLinearLayout.addView(innerLayout); 

     textListWord.add(qWordTV); 
     textListAnswer.add(aWordTV); 
     imageListAnswer.add(aImageView); 
     //c.moveToNext(); 

    } 
    while (c2.moveToNext()); 

這將dynamicaly創建視圖按照循環的狀態。

我想要下面的xml佈局中動態java代碼來創建它。

謝謝。

回答

相關問題