有2x2网格(动态使用TableLayout
)需要在其上显示图像.现在基于图像大小,意味着 - 如果图像适合1个细胞意味着1个细胞,那么大意味着2个细胞或4个细胞基于大小(我知道它将占用多少个细胞)
我可以在1个单元格中显示图像,但问题是如果图像需要2个单元格(第1列)如何在2个单元格中显示图像(没有干扰网格)
在不干扰电网,我看到的解决方法是动态地设置图像在上面你的TableLayout
.然后你可以存档这个:
我上传的测试项目的代码在这里 ;
初始化overlappingImage
,一旦需要将图像设置到单元格 - 您只需将其添加到布局中,并根据要填充的单元格数设置高度和宽度参数.
TableLayout
动态生成,单元格的布局xml:
该Activity
的布局:
以及处理按钮点击和生成表的活动代码:
public class MainActivity extends AppCompatActivity { RelativeLayout container; int cellWidth = 0, cellHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout); tableLayout.setStretchAllColumns(true); for (int i = 0; i < 4; i++) { TableRow tableRow = new TableRow(this); for (int j = 0; j < 10; j++) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View cell = layoutInflater.inflate(R.layout.table_cell, null, false); if (cellHeight == 0 ) { cell.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); cellWidth = cell.getMeasuredWidth(); cellHeight = cell.getMeasuredHeight(); } tableRow.addView(cell); } tableLayout.addView(tableRow); } container = (RelativeLayout)findViewById(R.id.container); overlappingImage = new ImageView(this); overlappingImage.setScaleType(ImageView.ScaleType.FIT_XY); } ImageView overlappingImage; private void restoreTableLayout() { container.removeView(overlappingImage); } public void onClick11(View view) { restoreTableLayout(); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(cellWidth, cellHeight); overlappingImage.setLayoutParams(params); overlappingImage.setImageResource(R.drawable.horizontal_cat); container.addView(overlappingImage); } public void onClick41(View view) { restoreTableLayout(); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(cellWidth*4, cellHeight); overlappingImage.setLayoutParams(params); overlappingImage.setImageResource(R.drawable.horizontal_cat); container.addView(overlappingImage); } public void onClick32(View view) { restoreTableLayout(); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(cellWidth*3, cellHeight*2); params.setMargins(cellWidth*2, cellHeight*2, 0 ,0); overlappingImage.setLayoutParams(params); overlappingImage.setImageResource(R.drawable.vertical_cat); container.addView(overlappingImage); } public void onClick22(View view) { restoreTableLayout(); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(cellWidth*2, cellHeight*2); overlappingImage.setLayoutParams(params); overlappingImage.setImageResource(R.drawable.horizontal_cat); container.addView(overlappingImage); } }
我希望,这有帮助.