Android对齐表格行中的视图

时间:2015-07-26 09:44:31

标签: android android-layout

我正在尝试以编程方式创建一个包含2个textviews,edittextview和按钮的表。我的按钮高度不等于其余元素。 我试过删除填充和边距但问题仍然存在。 任何人都可以指出错误或为此提供解决方法。

enter image description here  代码如下

    public void populateItems()
{
     // TODO : create uniform table row cells
      TableLayout tl = (TableLayout)findViewById(R.id.placeOrderTable);

      LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      lp.gravity=Gravity.CENTER_HORIZONTAL;
      lp.bottomMargin=5;

      LayoutParams lpTextView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);       
      lpTextView.weight=1;  
      lpTextView.width=0;
      lpTextView.bottomMargin=1;


      LayoutParams lpEditTextView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
      lpEditTextView.weight=1;  
      lpEditTextView.width=0;
    //  lpEditTextView.
      //lpEditTextView.
      lpEditTextView.gravity=Gravity.CENTER_HORIZONTAL;



      for(final HashMap<String,String> hm:mTodayItemsHashMap)
      {


      TableRow tr = new TableRow(getApplicationContext());
      tr.setLayoutParams(lp);


      TextView tvItemName = new TextView(getApplicationContext());
      tvItemName.setLayoutParams(lpTextView);
      tvItemName.setBackgroundResource(R.drawable.border_edittext);
      tvItemName.setText(hm.get("item_name").toUpperCase());
      tvItemName.setEms(10);
      tvItemName.setTextColor(Color.BLACK);
      tvItemName.setGravity(Gravity.CENTER);
     // evItemName.setHint(getString(R.string.item_name));

      TextView tvItemPrice = new TextView(getApplicationContext());
      tvItemPrice.setLayoutParams(lpTextView);
      tvItemPrice.setBackgroundResource(R.drawable.border_edittext);
      tvItemPrice.setTextColor(Color.BLACK);
      tvItemPrice.setText(hm.get("item_price").toUpperCase());
      tvItemPrice.setGravity(Gravity.CENTER);
      tvItemPrice.setEms(10);
     // evItemPrice.setHint(getString(R.string.item_price));


       final EditText evItemQty = new EditText(getApplicationContext());
      evItemQty.setLayoutParams(lpTextView);
      evItemQty.setInputType(InputType.TYPE_CLASS_NUMBER);
      evItemQty.setBackgroundResource(R.drawable.border_edittext);      
      evItemQty.setTextColor(Color.BLACK);
      evItemQty.setGravity(Gravity.CENTER);
      evItemQty.setEms(10);
      evItemQty.setFilters(new InputFilter[]{ new InputFilterMinMax("1", hm.get("max_qty"))});
     // evItemQty.setL
      evItemQty.setHint(getString(R.string.max_orders));


      Button btnAddToCart=new Button(getApplicationContext());
      btnAddToCart.setLayoutParams(lpTextView);
      btnAddToCart.setText("Add to Cart");
      btnAddToCart.setPadding(0, 0, 0, 0);
     // btnAddToCart.setLayoutParams(params);
     // btnAddToCart.setBackgroundResource(R.drawable.border_edittext);




      btnAddToCart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
            //  mCart.
                 item=new Item();
                  item.setName(hm.get("item_name"));
                  item.setPrice(hm.get("item_price"));
                  String temp=evItemQty.getText().toString();
                  try{
                      Integer.parseInt(temp);
                  }
                  catch(NumberFormatException nfe){
                      Toast.makeText(getApplicationContext(), "Please specify quantity",Toast.LENGTH_LONG);
                      return;
                  }
                  item.setQty(evItemQty.getText().toString());
                  if(mCart==null)
                     mCart=new Cart(); 
                mCart.add(item);
            }

        });     

      tr.addView(tvItemName);
      tr.addView(tvItemPrice);
      tr.addView(evItemQty);        
      tr.addView(btnAddToCart);
      tl.addView(tr,tl.getChildCount()-1, tl.getLayoutParams());
      }


}

1 个答案:

答案 0 :(得分:0)

您可能希望覆盖按钮的背景。你会得到这样的东西:enter image description here

这当然意味着你不再有弯曲的边缘了。这可以修复 正如this answer

中所述