【发布时间】:2015-03-17 21:47:22
【问题描述】:
我在滚动光标时以编程方式填充了一个 TableLayout。我在每个光标步骤中创建 3 个项目:TextView、ImageView 和 TextView。我的问题是 ImageView。
在 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);
}
}
【问题讨论】: