Java 如何调整包含在TableLayout行中的视图的高度
这是我在活动中得到的: 活动由表格布局填充。TableLayout依次由两个TableRow填充,这两个TableRow是包含100x100青色位图的两个视图。黄色区域是TableView位置1,1中活动的背景。红色区域是位置1,2中活动的背景 问题是我在位置2,1和2,2看不到另外两个青色位图。我认为问题在于第一行中的视图在宽度上调整了大小,但在高度上没有调整。因此,第一行中的视图遮挡了第二行中的视图 如果我将ViewGroup.LayoutParams或LayoutParams与WRAP_内容一起添加到视图中,我会得到一个java.lang.ArrithmeticException:除以零 这是我的代码:Java 如何调整包含在TableLayout行中的视图的高度,java,android,view,tablelayout,tablerow,Java,Android,View,Tablelayout,Tablerow,这是我在活动中得到的: 活动由表格布局填充。TableLayout依次由两个TableRow填充,这两个TableRow是包含100x100青色位图的两个视图。黄色区域是TableView位置1,1中活动的背景。红色区域是位置1,2中活动的背景 问题是我在位置2,1和2,2看不到另外两个青色位图。我认为问题在于第一行中的视图在宽度上调整了大小,但在高度上没有调整。因此,第一行中的视图遮挡了第二行中的视图 如果我将ViewGroup.LayoutParams或LayoutParams与WRAP_内
public class HIDActivity extends Activity
{
boolean toggle = false;
TableLayout IFaceLayout;
TableRow TButtons1;
TableRow TButtons2;
TableRow TArea;
SimpleButton TButton1;
SimpleButton TButton2;
SimpleButton TButton3;
SimpleButton TButton4;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
TButton1 = new SimpleButton(this);
TButton2 = new SimpleButton(this);
TButton3 = new SimpleButton(this);
TButton4 = new SimpleButton(this);
TButtons1 = new TableRow(this);
TButtons2= new TableRow(this);
TButtons1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
TButtons2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
TButtons1.addView(TButton1);
TButtons1.addView(TButton2);
TButtons2.addView(TButton3);
TButtons2.addView(TButton4);
IFaceLayout = new TableLayout(this);
IFaceLayout.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
IFaceLayout.setStretchAllColumns(true);
IFaceLayout.addView(TButtons1);
IFaceLayout.addView(TButtons2);
setContentView(IFaceLayout);
IFaceLayout.requestLayout();
}
class SimpleButton extends View
{
BitmapDrawable ButtonImage;
Paint Style;
int i , j ;
int width , heigth;
public SimpleButton(Context context)
{
super(context);
Style = new Paint();
if(toggle == false)
{
this.setBackgroundColor(Color.YELLOW);
toggle = true;
}
else
{
this.setBackgroundColor(Color.RED);
toggle = false;
}
}
@Override
public void onDraw(Canvas canvas)
{
ButtonImage = new BitmapDrawable(Bitmap.createBitmap(100 , 100 , Bitmap.Config.RGB_565));
this.width = ButtonImage.getBitmap().getWidth();
this.heigth = ButtonImage.getBitmap().getHeight();
for(i = 0 ; i < width ; i++)
{
for(j = 0 ; j < heigth ; j++)
{
ButtonImage.getBitmap().setPixel(i, j, Color.CYAN);
}
}
canvas.drawBitmap(ButtonImage.getBitmap() , 0 , 0 , Style);
}
}
}
有人能帮我解决这个问题吗。问题似乎是扩展视图的SimpleButton没有设置为垂直包装内容 设置布局参数时,您希望使用对象将驻留的容器。当您定义布局参数时,实际上是在说这就是我的对象/视图将如何填充其所在容器的空间。在这种情况下,您的按钮将位于表格行内,因此您应该定义相对于表格行的布局参数,如下所示:
TButton1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
TButton2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
TButton3. [same thing]
TButton4. [same thing]
您可能需要更改TButtons布局参数,以便与TableLayout相关,因为TableRow位于表格布局中
TButtons1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
TButtons2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
表布局也包含在父布局中。我不确定它是什么,但它可能是xml文件的根,这很可能是相对或线性布局。您应该根据表所在的父布局定义表布局参数
IFaceLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
我对地鼠的暗示有一部分是正确的
调用MyView.setLayoutParams并向其传递与包含MyView的对象类型匹配的布局对象是正确的。但是,为了允许容器控制包含对象的大小,我必须使用MATCH_PARENT而不是WRAP_内容
此外,我已部分解决了手动将高度设置为100的问题。即:
TButton1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT , 100);
现在我得到了4个正方形:
非常感谢。不幸的是,我这样做了:我试图用MATCH_PARENT更改WRAP_内容,但结果是之前的结果: