我希望我的问题不要太模糊。
我有问题,我被困了几天。我已经搜索了很多,尽管付出了努力,我还没有找到解决方案。
我的问题如下:
我有一个TableLayout,我用我在ArrayList中的项目总数动态填充我的数据。我的屏幕暂时看起来像这样:
但我想要的是以下内容:
我尝试了很多不同的选项:
由于我的努力和代码,我想坚持使用TableLayout。 GridLayout或任何其他..Layout也不是问题。
public void getTableLayoutDinnerProducts() {
tableLayout = (TableLayout)findViewById(R.id.tableLayoutDinner);
//gridLayout = (GridLayout)findViewById(R.id.gridLayoutDinner);
//gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
//gridLayout.setColumnCount(2);
//gridLayout.setRowCount(3);
//TableLayout.LayoutParams newTableLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
//newTableLayout.setMargins(400, 0, 0, 0);
// Creating a table row for each product in productController.
for (final Product product : productController.getStaticProducts()) {
if(product.getCategoryType() == CategoryType.Dinner){
// Create tablerows
tableRow = new TableRow(this);
tableRow.setId(product.getID() + 1);
tableRow.setBackgroundColor(Color.WHITE);
tableRow.setLayoutParams(new Toolbar.LayoutParams(300, 300));
// Create content for tablerows
textArray = new TextView(this);
textCaloriesArray = new TextView(this);
textProductArray = new TextView(this);
// Add image for each row
image = new ImageView(this);
bitmap = BitmapFactory.decodeResource(getResources(), product.getImage());
int width=400;
int height=400;
resizedbitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
image.setImageBitmap(resizedbitmap);
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
//image.setBackgroundResource(R.drawable.imagestyle);
image.setAdjustViewBounds(true);
tableRow.addView(image, 0, new TableRow.LayoutParams(400, 400));
textArray.setText(product.getName());
textCaloriesArray.setText(" Cal: " + String.valueOf(product.getCalories()));
textProductArray.setText(" Prod: " + String.valueOf(product.getProductType()));
textArray.setTextSize(18);
textArray.setTypeface(null, Typeface.BOLD);
textArray.setTextColor(Color.BLACK);
textArray.setPadding(0, 150, 0, 100);
//TableRow.LayoutParams params = (TableRow.LayoutParams)textArray.getLayoutParams();
//params.span = 4;
//textArray.setLayoutParams(params); // causes layout update
tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
tableRow.addView(textArray, 1, new TableRow.LayoutParams(200, 500));
tableRow.addView(textCaloriesArray, 2, new TableRow.LayoutParams(200, 400));
tableRow.addView(textProductArray, 3, new TableRow.LayoutParams(200, 400));
tableRow.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final View background = v;
background.setBackgroundColor(Color.RED);
final Dialog dialog = new Dialog(DinnerScreen.this);
dialog.setContentView(R.layout.popup);
dialog.show();
dismisspopup = (Button)dialog.findViewById(R.id.dismissPopup);
dismisspopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
background.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
}
});
removeproduct = (Button)dialog.findViewById(R.id.removeProduct);
removeproduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DinnerScreen.class);
dialog.dismiss();
products = productController.getStaticProducts();
products.remove(product);
productController.setStaticProducts(products);
productController.getStaticProducts();
intent.putExtra("totalProducts", products);
intent.putExtra("totalCalories", String.valueOf(calorieCounter.getCountcalories()));
overridePendingTransition( 0, 0);
startActivity(intent);
overridePendingTransition( 0, 0);
showToastMessage("Product has been removed");
}
});
return false;
}
});
tableRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calorieCounter.addCalories(product.getCalories());
changeStatusBar(calorieCounter.getCountcalories());
}
});
//gridLayout.addView(tableRow);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
}
因为我动态添加这些,所以我没有.XML文件来添加宽度或位置。
必须有解决此问题的方法。只有我再也找不到了。
提前致谢