简体   繁体   中英

Align single column in table row android

I am trying to align row in table layout pragmatically, but the last column where i have image button keep stretching, I tried padding and setColumnStretchable to false but no luck so far. Below is my code and app UI...

在此处输入图片说明 .

 TableLayout tl = (TableLayout) findViewById(R.id.scrollViewTable);
    tl.setColumnStretchable(1,true);
    tl.setColumnStretchable(2,true);
    tl.setColumnStretchable(3,true);
    tl.setColumnStretchable(4,false);
    /* Create a new row to be added. */
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
    /* Create a Button to be the row-content. */
    TextView prodName = new TextView(this);
    TextView unitPrice = new TextView(this);
    TextView Quantity = new TextView(this);
    TextView total = new TextView(this);
    Button remove = new Button(this);

    prodName.setText(productDetails.getText());
    unitPrice.setText(price.getText());
    Quantity.setText(sellQuantity.getText());
    total.setText("890");
    remove.setBackgroundResource(R.drawable.delete);
    prodName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    prodName.setGravity(Gravity.CENTER);
    unitPrice.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    unitPrice.setGravity(Gravity.CENTER);
    Quantity.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    Quantity.setGravity(Gravity.CENTER);
    remove.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    remove.setPadding(1,1,1,1);
    /* Add Button to row. */
        tr.addView(prodName);
    tr.addView(unitPrice);
    tr.addView(Quantity);
    tr.addView(remove);

    /* Add row to TableLayout. */
        tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

Found solution but its not really that clean the Button still doesn't look perfect aligned. Let me know if you know any better way to manage this..

在此处输入图片说明

Replace Line
remove.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
    remove.setPadding(1,1,1,1);

Added this
remove.setLayoutParams(new TableRow.LayoutParams(0,TableRow.LayoutParams.WRAP_CONTENT,0f));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM