How does TableLayout work with TableRow?
I have a TableLayout filled programmatically while scrolling a cursor. I create 3 items every cursor step: TextView, ImageView and TextView. My problem is with the ImageView.
On ImageView I wanna draw a ShapeDrawable (for that I have overridden a View like 2D Graphics - Shape Drawable).
On ImageView I set a background resource (to apply shape properties) with a drawable XML file. For that I want the height to differ from the other TextView on the same TableRow.
I tried various combinations (setting LayoutParams, gravity, layout_weight) without success. The result is always the same: the ImageView is the same height as the TableRow.
Here some pieces of code:
TableLayout tl = (TableLayout)findViewById(R.id.list_show_stats);
tl.setColumnStretchable(1, true);
tl.setColumnShrinkable(0, true);
for every cursor step
TableRow tr = new TableRow(this);
TableRow.LayoutParams tr_lp = new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(tr_lp);
tr.setBaselineAligned(true);
TextView tv1 = new TextView(this);
LayoutParams tv1lp = new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, 1L);
tv1lp.column = 0;
tv1lp.gravity = Gravity.LEFT|Gravity.CENTER_VERTICAL;
tv1.setLayoutParams(tv1lp);
tv1.setText(" description ");
ImageView img = new ImageView(this);
CustomImgBarDrawableView dw = new CustomImgBarDrawableView(this, width, height);
img.setImageDrawable(dw.mDrawable);
LayoutParams lp = new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, 1L);
lp.gravity = Gravity.LEFT;
lp.column = 1;
img.setLayoutParams(lp);
img.setBackgroundResource(R.drawable.layout_showbar);
TextView tv2 = new TextView(this);
LayoutParams tv2lp = new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, 1L);
tv2lp.column = 2;
tv2lp.gra开发者_运维问答vity = Gravity.RIGHT|Gravity.CENTER_VERTICAL|Gravity.FILL_HORIZONTAL;
tv2.setLayoutParams(tv2lp);
tv2.setText(" value ");
tr.addView(tv1);
tr.addView(img);
tr.addView(tv2);
tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT))
public class CustomImgBarDrawableView extends View {
private ShapeDrawable mDrawable;
public CustomImgBarDrawableView(Context context, int w, int h) {
super(context);
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.setIntrinsicHeight(h);
mDrawable.setIntrinsicWidth(w);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}
Thanks anyway.
I have found another solution.
For to show a progressive value, instead of single ImageView with defined dimensions,
I use a horizontal LinearLayout with x-many ImageView fill-fill.
So I have solved my problem of positioning, delegating Android the work of correctly fill the space.
Although you have found another solution but you can use
img.setLayoutParams(new TableRow.LayoutParams(width,height));
精彩评论