带表格布局的 3*3 网格

发布于 2024-09-17 01:06:37 字数 1158 浏览 5 评论 0原文

我想要一个 3*3 网格,每个单元格中都有一个图像按钮 我已经完成了下面的代码

TableLayout tl=new TableLayout(this);
int intNbRows=3;
int intNbCols=3;
for (int i = 0 ; i < intNbRows;i++)
{
   TableRow tr=new TableRow(this);
   tr.setMinimumHeight(hauteur/intNbRows);//TODO : a voir 
   tr.setMinimumWidth(largeur/intNbCols); //TODO : a voir
   tr.setId(100+i);

   for (int j = 0 ; j < intNbCols;j++)
   {
      int numImage=((i*intNbCols)+j+1);
      final ImageButton myButton=new ImageButton(this);
      Drawable d=Drawable.createFromPath("/sdcard/b" + numImage  + ".png");
      myButton.setBackgroundDrawable(d);
      myButton.setId(200+numImage);
      myButton.setOnClickListener(this);
      myButton.setPadding(0, 0, 0, 0);
      myButton.setScaleType(ScaleType.CENTER_INSIDE);
      tr.addView(myButton);
   }
   tr.setLayoutParams(new TableRow.LayoutParams(largeur,hauteur/intNbRows));
   tl.addView(tr);
}
tl.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));           
this.setContentView(tl);

使用此代码,行具有良好的高度,但宽度不合适,并且 ImageButtons 未在单元格中居中......

有什么想法吗?

I want a 3*3 grid with an ImageButton centered in each cell
I've done the code below

TableLayout tl=new TableLayout(this);
int intNbRows=3;
int intNbCols=3;
for (int i = 0 ; i < intNbRows;i++)
{
   TableRow tr=new TableRow(this);
   tr.setMinimumHeight(hauteur/intNbRows);//TODO : a voir 
   tr.setMinimumWidth(largeur/intNbCols); //TODO : a voir
   tr.setId(100+i);

   for (int j = 0 ; j < intNbCols;j++)
   {
      int numImage=((i*intNbCols)+j+1);
      final ImageButton myButton=new ImageButton(this);
      Drawable d=Drawable.createFromPath("/sdcard/b" + numImage  + ".png");
      myButton.setBackgroundDrawable(d);
      myButton.setId(200+numImage);
      myButton.setOnClickListener(this);
      myButton.setPadding(0, 0, 0, 0);
      myButton.setScaleType(ScaleType.CENTER_INSIDE);
      tr.addView(myButton);
   }
   tr.setLayoutParams(new TableRow.LayoutParams(largeur,hauteur/intNbRows));
   tl.addView(tr);
}
tl.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));           
this.setContentView(tl);

With this code, rows have the good height, but not width, and ImageButtons are not centered in cells....

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唔猫 2024-09-24 01:06:37

好的,我需要将表格行放入线性布局中,效果非常好:)

OK i need to put tablerows in linearlayout, and that works very well :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文