Android TableLayout动态创建(行标题向下推)

时间:2014-04-15 17:30:23

标签: android tablelayout android-tablelayout

我已经动态创建了表格布局。但是,当动态创建其他行时,我的标题行会向下推。我想让冠军保持在最顶层。

enter image description here

XML代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstScreen" 
android:orientation="vertical"
android:background="@color/white" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:text="Shopping Cart"
    android:textColor="#898989"
    android:textSize="17dp"
    android:textStyle="bold" />


<TableLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/displayLinear"
    android:layout_marginLeft="10dp"
    android:background="#ffffff"
    android:orientation="vertical" >

</TableLayout>

<Button
    android:id="@+id/second"
    style="@style/btnStyleShakespeare"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="80dp"
    android:layout_marginTop="10dp"
    android:text="Checkout"
    android:textColor="#ffffff" />

<Button
    android:id="@+id/scanMore"
    style="@style/btnStyleShakespeare"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="80dp"
    android:layout_marginTop="5dp"
    android:text="Scan More"
    android:textColor="#ffffff" />

</LinearLayout>

这是我动态创建的代码。

package info.androidhive.slidingmenu;



public class ScreenFirstFragment extends Fragment {

public ScreenFirstFragment(){}
public static double pFinalPrice;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_firstscreen, container, false);
    //final LinearLayout lm = (LinearLayout) rootView.findViewById(R.id.linearMain);
    final Button secondBtn = (Button) rootView.findViewById(R.id.second);
    final Button scanMoreBtn = (Button) rootView.findViewById(R.id.scanMore);

    //Get Global Controller Class object (see application tag in AndroidManifest.xml)
    final Controller aController = (Controller) getActivity().getApplicationContext();

    /******* Create view elements dynamically and show on activity ******/

    //Product arraylist size
    int ProductsSize = aController.getProductsArraylistSize();

    /******** Dynamically create view elements - Start **********/

    TableLayout ll = (TableLayout) rootView.findViewById(R.id.displayLinear);
    ll.setStretchAllColumns(true);
    ll.setShrinkAllColumns(true);

    TableRow rowProdLabels = new TableRow(this.getActivity());

    // Product column
    TextView prodLabel = new TextView(this.getActivity());
    prodLabel.setText("PRODUCT");
    prodLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
    rowProdLabels.addView(prodLabel);

    // Price  column
    TextView priceLabel = new TextView(this.getActivity());
    priceLabel.setText("PRICE");
    priceLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
    rowProdLabels.addView(priceLabel);

    // Quantity column
    TextView quantityLabel = new TextView(this.getActivity());
    quantityLabel.setText("Quantity");
    quantityLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
    rowProdLabels.addView(quantityLabel);

    // Total column
    TextView totalLabel = new TextView(this.getActivity());
    totalLabel.setText("Total Price");
    totalLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
    rowProdLabels.addView(totalLabel);

    ll.addView(rowProdLabels);

    for(int j=0;j< ProductsSize;j++)
    {   
        // Get probuct data from product data arraylist
        String pName = aController.getProducts(j).getProductName();
        double pPrice   = aController.getProducts(j).getProductPrice();
        int pQuantity   = aController.getProducts(j).getProductQuantity();
        double pTotalPrice = pPrice * pQuantity;

        TableRow row= new TableRow(this.getActivity());

        TextView product = new TextView(this.getActivity());
        product.setText(pName+"    ");

        //Add textView to LinearLayout
        row.addView(product);

        TextView price = new TextView(this.getActivity());
        price.setText("RM "+pPrice+"     ");

        //Add textView to LinearLayout
        row.addView(price);

        TextView quantity = new TextView(this.getActivity());
        quantity.setText(pQuantity+"     ");

        //Add textView to LinearLayout
        row.addView(quantity);

        TextView totalprice = new TextView(this.getActivity());
        totalprice.setText(String.format("RM %.2f",pTotalPrice));

        //Add textView to LinearLayout
        row.addView(totalprice);

        //Update final price
        pFinalPrice += pTotalPrice;

        ll.addView(row,j);          
    }


    /******** Dynamically create view elements - End **********/

    secondBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

             FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
             mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
             mFragmentTransaction.replace(R.id.frame_container, new ScreenSecondFragment(), "Cart");
             mFragmentTransaction.commit();

        }
    }); 

    scanMoreBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

             FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
             mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
             mFragmentTransaction.replace(R.id.frame_container, new ScanFragment(), "Scan Product");
             mFragmentTransaction.commit();
        }
    });  
    return rootView;
}
}

0 个答案:

没有答案