我有一個TableLayout編程方式滾動遊標時填充。我爲每個遊標步驟創建3個項目:TextView,ImageView和TextView。我的問題是與ImageView。TableLayout如何與TableRow一起使用?
在ImageView上我想繪製一個ShapeDrawable(爲此我重寫了一個像2D Graphics - Shape Drawable的視圖)。
在ImageView上,我使用可繪製的XML文件設置背景資源(應用形狀屬性)。爲此,我希望高度不同於同一TableRow上的其他TextView。
我嘗試過各種組合(設置LayoutParams,gravity,layout_weight),但沒有成功。結果總是相同的:ImageView與TableRow的高度相同。
這裏的一些代碼片段:
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.gravity = 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);
}
}