【问题标题】:How does TableLayout work with TableRow?TableLayout 如何与 TableRow 一起使用?
【发布时间】: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);
  }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    还是谢谢。
    我找到了另一个解决方案。
    为了显示一个渐进的值,而不是具有定义尺寸的单个 ImageView,
    我使用带有 x-many ImageView 填充填充的水平 LinearLayout。
    所以我已经解决了我的定位问题,委派了Android正确填充空间的工作。

    【讨论】:

      【解决方案2】:

      虽然你找到了另一种解决方案,但你可以使用

      img.setLayoutParams(new TableRow.LayoutParams(width,height));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-12
        • 2014-10-01
        • 2012-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-14
        相关资源
        最近更新 更多