增加动态创建的tablelayout的tablerow宽度

时间:2012-06-30 16:03:04

标签: android

我已经创建了一个表格布局,其中我的表格行高度太小,所以我想增加表格行的高度,请指导我动态创建表格,所以我不知道。

代码:我试图在Latoutparam整数中指定宽度但不能正常工作

public void createTableLayout()
{
    String status="Pending";
    TextView txtid=(TextView)findViewById(R.id.txtcid);
    //TextView txtdate=(TextView)findViewById(R.id.txtDate);
    txtid.setText("Customer Id/Name : "+shopnum+"/"+shopname1);
    btnorder1=(Button)findViewById(R.id.btnOrder123);
    btnBack1=(Button)findViewById(R.id.btnBack1);
    txtTotalAmount=(TextView)findViewById(R.id.txtTotalAmt);
    btnorder1.setOnClickListener(new View.OnClickListener()
    {       
        @Override
        public void onClick(View v) 
        {
            ShowcOrder1();
            int rcount=(table).getChildCount();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            String currentDateandTime = sdf.format(new Date());
            for(int r=1;r<rcount;r++)
            {
                TableRow row=(TableRow)table.getChildAt(r);
                int vcount=(row).getChildCount();
            //  String str=
                for(int v1=0;v1<vcount;v1++)
                {
                    TextView tv = (TextView)(((TableRow)row)).getChildAt(v1);


    insertIntoOrderDetails(String.valueOf(orderid),ProdcutId,PackageId,Quantity);
                }
                insertIntoOrder(String.valueOf(orderid), currentDateandTime, shopnum,String.valueOf(roundtotal), sid);
                Toast.makeText(getApplicationContext(), "Order Placed",Toast.LENGTH_LONG).show();
                finish();
            //  Toast.makeText(getApplicationContext(), "round: "+roundtotal,Toast.LENGTH_LONG).show(); 
            }

        });

        btnBack1.setOnClickListener(new View.OnClickListener()
        {   
            @Override
            public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub
                finish();
            }
        });

        //txtdate.setText("Order Date is : "+odate);
            table = (TableLayout) findViewById(R.id.tbpastorderdetails);
            TableRow tr_heading = new TableRow(Pastorder.this);
            tr_heading.setId(10);
            tr_heading.setBackgroundColor(Color.BLACK);
            tr_heading.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

            TextView label_product_id = new TextView(Pastorder.this);
            label_product_id.setId(20);
            label_product_id.setText("PID");
            label_product_id.setTextColor(Color.WHITE);
            //label_question.setPadding(2, 2, 2, 2);
            // label_date.setPadding(5, 5, 5, 5);
            tr_heading.addView(label_product_id); // add the column to the table row

            //TextView label_question = new TextView(this);
            TextView label_package_id = new TextView(Pastorder.this);
            label_package_id.setId(20);
            label_package_id.setText("  Pack.ID");
            label_package_id.setTextColor(Color.WHITE);
            //label_question.setPadding(2, 2, 2, 2);
            // label_date.setPadding(5, 5, 5, 5);
            tr_heading.addView(label_package_id); // add the column to the table row


            //TextView label_question = new TextView(this);
            TextView label_product = new TextView(Pastorder.this);
            label_product.setId(20);
            label_product.setText(" PRODUCT");
            label_product.setTextColor(Color.WHITE);
            //label_question.setPadding(2, 2, 2, 2);
            // label_date.setPadding(5, 5, 5, 5);
            tr_heading.addView(label_product); // add the column to the table row
                                                // here
        //  Toast.makeText(ExaminationActivity.this,"hi try ",Toast.LENGTH_LONG).show();
            //TextView label_answer = new TextView(this);
            TextView label_catagory = new TextView(Pastorder.this);
            label_catagory.setId(21);// define id that must be unique
            label_catagory.setText("    CATAGORY"); // set the text for the header
            label_catagory.setTextColor(Color.WHITE); // set the color
            // label_weight_kg.setPadding(5, 5, 5, 5); // set the padding (if
            // required)
        //  label_weight_kg.setPadding(2, 2, 2, 2);
            tr_heading.addView(label_catagory); // add the column to the table row
                                                    // here


            //TextView label_youranswer = new TextView(this);
            TextView label_package = new TextView(Pastorder.this);
            label_package.setId(20);
            label_package.setText(" PACKAGE");
            label_package.setTextColor(Color.WHITE);
            //label_question.setPadding(2, 2, 2, 2);
            // label_date.setPadding(5, 5, 5, 5);
            tr_heading.addView(label_package); // add the column to the table row
                                                // here
            TextView label_weight = new TextView(Pastorder.this);
            label_weight.setId(20);
            label_weight.setText("  WEIGHT");
            label_weight.setTextColor(Color.WHITE);
            //label_question.setPadding(2, 2, 2, 2);
            // label_date.setPadding(5, 5, 5, 5);
            tr_heading.addView(label_weight); // add the column to the table row

            TextView label_qty = new TextView(Pastorder.this);
            label_qty.setId(20);
            label_qty.setText(" QUANTITY");
            label_qty.setTextColor(Color.WHITE);
            //label_question.setPadding(2, 2, 2, 2);
            // label_date.setPadding(5, 5, 5, 5);
            tr_heading.addView(label_qty); // add the column to the table row

            TextView label_amt = new TextView(Pastorder.this);
            label_amt.setId(20);
            label_amt.setText(" AMOUNT");
            label_amt.setTextColor(Color.WHITE);
            //label_question.setPadding(2, 2, 2, 2);
            // label_date.setPadding(5, 5, 5, 5);
            tr_heading.addView(label_amt); // add the column to the table row       
            table.addView(tr_heading, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }

public void addRows()
{
    try
    {               
        msaledb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = msaledb.rawQuery("SELECT * FROM " + PRODUCT_TABLE+" WHERE Product_ID = '" + pid +"'", null);
        Cursor allrows1  = msaledb.rawQuery("SELECT * FROM " + PACKAGING_TABLE ,null);
        if(allrows.moveToFirst() && allrows1.moveToFirst())// && allrows1.moveToFirst())
         {         
            int i=1;    
            do
            {   
                tr = new TableRow(Pastorder.this);
                tr.setId(i);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

                TextView tvpid = new TextView(Pastorder.this);
                tvpid.setId(11);
                tvpid.setText(""+pid);
                tvpid.setPadding(2, 0, 5, 0);
                tvpid.setTextColor(Color.BLACK);
                tr.addView(tvpid);

                TextView tv_pack_id = new TextView(Pastorder.this);
                tv_pack_id.setId(12);
                tv_pack_id.setText("    "+packid);
                tv_pack_id.setPadding(2, 0, 5, 0);
                tv_pack_id.setTextColor(Color.BLACK);
                tr.addView(tv_pack_id);

                TextView tv = new TextView(Pastorder.this);
                tv.setId(1);
                tv.setText("    "+allrows.getString(2));
                tv.setPadding(2, 0, 5, 0);
                tv.setTextColor(Color.BLACK);
                tr.addView(tv);
                TextView tv1 = new TextView(Pastorder.this);
                tv1.setId(2);
                tv1.setText("   "+allrows.getString(3));
                tv1.setPadding(2, 0, 5, 0);
                tv1.setTextColor(Color.BLACK);
                tr.addView(tv1);
                do
                {
                if(allrows1.getString(1).trim().equals(packid.trim()) && allrows1.getString(2).trim().equals(pid.trim()))
                {
//                      Toast.makeText(getApplicationContext(), ""+allrows1.getString(2).trim(),Toast.LENGTH_LONG).show();
//                      Toast.makeText(getApplicationContext(), ""+p_id.get(0).trim(),Toast.LENGTH_LONG).show();
                        TextView tv2 = new TextView(Pastorder.this);
                        tv2.setId(3);
                        tv2.setText("   "+allrows1.getString(3));
                        tv2.setPadding(2, 0, 5, 0);
                        tv2.setTextColor(Color.BLACK);
                        tr.addView(tv2);                        
                        TextView tv3 = new TextView(Pastorder.this);
                        tv3.setId(4);
                        tv3.setText("   "+allrows1.getString(4));
                        tv3.setPadding(2, 0, 5, 0);
                        tv3.setTextColor(Color.BLACK);
                        tr.addView(tv3);                        
                        TextView tv4 = new TextView(Pastorder.this);
                        tv4.setId(4);
                        tv4.setText("   "+qty);
                        tv4.setPadding(2, 0, 5, 0);
                        tv4.setTextColor(Color.BLACK);
                        tr.addView(tv4);                        
                        TextView tv5 = new TextView(Pastorder.this);
                        tv5.setId(4);
                        tv5.setText("   "+Integer.parseInt(allrows1.getString(6).trim())*Integer.parseInt(qty.trim()));
                        tv5.setPadding(2, 0, 5, 0);
                        tv5.setTextColor(Color.BLACK);
                        tr.addView(tv5);

                }
                }while(allrows1.moveToNext());

                table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                i++;
                tr.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        view.setBackgroundColor(Color.LTGRAY);
                       // Toast.makeText(getApplicationContext(),""+tr.getId(),Toast.LENGTH_LONG).show();
                        tr1=(TableRow)view; //assign selected TableRow gobally
                        registerForContextMenu(tr1);
                        openContextMenu(tr1);
                    }
                });

            }while(allrows.moveToNext());

           }

    }
    catch(Exception e)
    {
          Toast.makeText(getApplicationContext(), "Error in search", Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:2)

TableRow布局参数必须是换行内容。尝试在TableRow对象内的视图中手动设置相应的布局参数,而不是尝试扩展行以满足您的需要。包裹内容或填充父级将无效,请尝试使用实际密度像素。

希望这有帮助