Android:如何动态创建tableView,如下面的xml代码?

时间:2012-01-09 06:20:51

标签: android xml android-layout android-emulator android-ndk

在我的应用程序中,我想动态创建TableView: 在这里,我有xml视图,就像我想要Andriod中的Dynamicaly一样。

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>

我创建了动态视图,如下面的代码所示:

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

这将根据循环的条件动态创建视图。

我希望下面的XML布局能够动态创建Java代码。

感谢。

1 个答案:

答案 0 :(得分:1)

嗨看看这个链接应该有你想要的!!! http://wowjava.wordpress.com/2011/04/01/dynamic-tablelayout-in-android/