【问题标题】:Resizing Button in a TableLayout, Dynamically (Android)动态调整 TableLayout 中的按钮大小 (Android)
【发布时间】:2014-03-11 19:24:26
【问题描述】:

好的,我已经搜索了它的解决方案,但是找不到。问题很简单。 Android 中有一个TableLayout,我在其中添加了几行。在最后一行,我添加了一个按钮。现在,问题是按钮的大小不是恒定的。我的意思是,它填满了列。如果上述数据占用了较长的列宽,则按钮的宽度也会相应增加。我也尝试过LayoutParamssetHeightsetWidth 功能,但这对我没有帮助..

如果有人能帮助我,我将不胜感激。

TableLayout table=new TableLayout(this);
TableRow tableRow=new TableRow(this);
Button button=new Button(this);
TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 
Button.setLayoutParams(trParams); 
tableRow.AddView(button); 
table.addView(tableRow);

【问题讨论】:

  • 分享你的xml文件....
  • 我正在动态地执行此操作,即以编程方式。
  • TableLayout table=new TableLayout(this); TableRow tableRow=new TableRow(this);按钮按钮=新按钮(此); TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); Button.setLayoutParams(trParams); tableRow.AddView(按钮); table.addView(tableRow);

标签: android button tablelayout tablerow layoutparams


【解决方案1】:

您在按钮而不是按钮上调用了 setLayoutParams。 名称区分大小写。 Button 是类,而 button 是您的对象。 试试:

TableLayout table=new TableLayout(this);
TableRow tableRow=new TableRow(this);
Button button=new Button(this);
TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); 
tableRow.AddView(button, trParams); 
table.addView(tableRow);

但是,这并不能解决主要问题。根据参考文档,TableRow 的所有子项都被强制为 MATCH_PARENT 的宽度。

您可以尝试将您的按钮嵌套在另一个布局中。例如,将您的按钮放在水平 LinearLayout 中,然后将 LinearLayout 放入 TableRow。

【讨论】:

  • TableLayout table=new TableLayout(this); TableRow tableRow=new TableRow(this);按钮按钮=新按钮(此); TableRow.LayoutParams trParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT); Button.setLayoutParams(trParams); tableRow.AddView(按钮); table.addView(tableRow);
猜你喜欢
  • 1970-01-01
  • 2012-10-28
  • 2019-11-20
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 2011-05-30
相关资源
最近更新 更多