开发者

Measure column width in TableLayout

I have a TableLayout with 4 columns. In order to stretch column 1 I do:

android:stretchColumns="1"

This works well. Now I need to measure the width of this column 1 once it's displayed on the screen. I need to do this because I want to fit a custom view into this column. This custom view is just a rectangle whic开发者_如何转开发h shall fill into the complete width of this column 1. I draw the rectangle by:

public class TaskRectangleView extends View {
private ShapeDrawable mDrawable;
private Paint textPaint;    
private String task;

public TaskRectangleView(Context context, String task, int width, int height) {
    super(context);

    this.task = task;

    int x = 10;
    int y = 10;                

    mDrawable = new ShapeDrawable(new RectShape());
    int color = DbSettingsManager.getInstance(context).getColorByName(task);
    mDrawable.getPaint().setColor(color);
    mDrawable.setBounds(x, y, x + width, y + height); 

    textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(10);        
}

protected void onDraw(Canvas canvas) {
    mDrawable.draw(canvas);

    canvas.drawText(this.task, 10, 20, textPaint);
}

}

So this custom drawn rectangle should fit into column 1 of my TableView. For this (so I believe) I need to measure the width of the column at display-time. How to do this? Thanks!


In order to set a child view's width as 80%, you can use the "layout_width" attribute, set it to 0.8


I don't think you need to know the dimensions of your parent. You could use the match_parrent attribute at your custom view. The article How Android draws views describes how to make your custom view the right size.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜