Параметры макета для динамически создаваемых кнопок внутри TableRow - PullRequest
0 голосов
/ 15 октября 2019

Я занят приложением для Android и у меня проблемы с расположением кнопок в TableRow.

У меня есть TableLayout в моем XML. Табличные строки, которые я динамически создаю / вставляю с информацией, которая в настоящее время находится в моем массиве, также в то же время я динамически создаю 2xbuttons для этой TableRow.

Будет отображаться строка таблицы: ItemDescription ItemQty ItemCost -butPlus- -butMinus- QtySelected

Все отображается правильно, однако у меня возникла проблема с параметрами макета созданных кнопок.

1-я кнопка (butPlus) отображается в соответствии с заданными параметрами макета,однако вторая кнопка (butMinus) не отражает указанные параметры макета. На ней показана кнопка с параметрами макета, не указанными мной. Некоторые значения по умолчанию?

Может кто-нибудь помочь определить, почему вторая кнопка не сгенерирована, как указано.

Я также попытался указать параметры макета для кнопок глобально и локально, но это не имело никакого значения. По-прежнему действуют только 1-я кнопка и 2-я кнопка остается неизменной.

// Method: Create rows for each stock item that has a qty more than 0 and add it // to the table
// Each row contains a description,  selling price, qty available, buttons for 
// + and - & qty selected for sale transaction
// Each row also has a hidden stock item ID field
// Var minusQtySold: This is the number of items marked as sold in this 
// transaction for display 

private TableRow createStockItemRowForSale(StockItem stockItem, int 
                 minusQtySold) {
//Check if this stock item has any qty is not zero
if (stockItem.getUnits() < 1) return null;

//Create new table row and add text views for description, selling price, 
//qty available, buttons for + and - & qty selected for sale transaction
TableRow row = new TableRow(getContext());

// Set rowIDNumber to be same as current StockItemID
row.setId(stockItem.getID());
row.setLayoutParams(new 
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
TableLayout.LayoutParams.MATCH_PARENT));

//Create TextView for description
TextView stockDescrip = new TextView(getContext());
stockDescrip.setWidth(200);
stockDescrip.setText(stockItem.getDescription());        
stockDescrip.setTextColor(getResources().getColor(R.color.colorDefaultBlack));
row.addView(stockDescrip);
//create TextView for selling price
TextView stockSalesPrice = new TextView(getContext());
stockSalesPrice.setWidth(200);
stockSalesPrice.setText(String.valueOf(stockItem.getSalePrice()));        stockSalesPrice.setTextColor(getResources().getColor(R.color.colorDefaultBlack));
row.addView(stockSalesPrice);
//create TextView for current stock item qty in inventory
TextView stockUnits = new TextView(getContext());
stockUnits.setTag("qtyAvailable");
stockUnits.setWidth(200);
stockUnits.setText(String.valueOf(stockItem.getUnits() - minusQtySold));   stockUnits.setTextColor(getResources().getColor(R.color.colorDefaultBlack));
row.addView(stockUnits);

//Set Buttons layout parameters : width and height / layout margins between //buttons
TableRow.LayoutParams butParams = new TableRow.LayoutParams(110, 110);
butParams.setMargins(5, 3, 5, 3);
//Create a + button and set it's id to ("+" + StockItem.Id)
Button butPlus = new Button(getContext());
butPlus.setId(stockItem.getID() + positiveButtons); 
//eg: 1000022 = +Button or stockItem#22
butPlus.setLayoutParams(butParams);
butPlus.setText("+");        
butPlus.setTextColor(getResources().getColor(R.color.colorDefaultBlack));        
butPlus.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
row.addView(butPlus);

//Create an onClickListener for butPlus
butPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// call method plusMinusButtonHandler
plusMinusButtonHandler(view);}});

//Create a - button and set it's id to ("-" + StockItem.Id)
Button butMinus = new Button(getContext());
butMinus.setId(stockItem.getID() + negativeButtons); 
//eg: 2000022 = -Button for stockItem#22
butMinus.setText("-");
butMinus.setLayoutParams(butParams);        butMinus.setTextColor(getResources().getColor(R.color.colorDefaultBlack));        butMinus.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
row.addView(butMinus);

//Create an onClickListener for butPlus
butMinus.setOnClickListener(new View.OnClickListener() {            
@Override
public void onClick(View view) {
// call method plusMinusButtonHandler
plusMinusButtonHandler(view);}});

//TextView that shows the selected qty for sale (based on +,- buttons pressed)
TextView qtySelected = new TextView(getContext());
qtySelected.setTag("qtySelected");
qtySelected.setWidth(200);
qtySelected.setText(String.valueOf(minusQtySold));
qtySelected.setTextColor(getResources().getColor(R.color.colorDefaultBlack));
row.addView(qtySelected);

//Return the newly created TableRow object
return row;
}//end createStockItemRowForSale
...