0

我制作了一个“GraphBar”自定义视图,它的底部是 a RelativeLayout,其上方是高度不同的 。这是代码:TextViewImageView

public class GraphBar extends RelativeLayout {

    private int mAvailHeight; // space for the bar (component minus label)
    private float mBarHeight; // 0.0-1.0 value

    public GraphBar(Context context) {
        this(context, null);
    }

    public GraphBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GraphBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.graphbar, this, true);
        setId(R.id.graphBar); // defined in <merge> but not assigned (?)
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mAvailHeight = getHeight()-findViewById(R.id.label).getHeight();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        View bar2 = findViewById(R.id.smallBar);
        RelativeLayout.LayoutParams llp2 = (RelativeLayout.LayoutParams) bar2.getLayoutParams();
        llp2.height = Math.round((float)mAvailHeight * mBarHeight);
    }

    public void setBarHeight(float value, float max) {
        mBarHeight = value / max;
        findViewById(R.id.smallBar).requestLayout();
    }

    public void setLabel(CharSequence c) {
        ((TextView) findViewById(R.id.label)).setText(c);
    }
}

虽然添加这些 GraphBars 并设置它们的高度onCreate()效果很好,但如果我创建它们 onClickSomething 或setBarHeight()在创建的栏上再次调用,查看更改的唯一方法是加载视图层次结构。我在这里被告知这意味着我需要打电话给某个地方requestLayout()。除了修改后还有什么地方mBarHeight?有什么帮助吗?我到处都试过了,invalidate()也试过了。

谢谢你,安德里亚

(如果您需要,我可以发布我进行测试的活动和 graphbar.xml)


I've found it's probably a bug. The workaround should be, again, calling requestLayout(). Still i don't understand where i can put the call.

4

1 回答 1

0

I've finally found a way to call again requestLayout(). I called setWillNotDraw(false) in the constructor so that in onDraw() (that is after onLayout()) I can call the extra requestLayout(). This generates a stupid cycle but solves the problem aesthetically.

If anyone knows a better solution, let me know... here the new code (modifies are next to comments):

//...
    public GraphBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.graphbar, this, true);
        setWillNotDraw(false); // WORKAROUND: onDraw will be used to make the
                                // redundant requestLayout() call
        setId(R.id.graphBar);
    }
//...
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // i think it's one of the worst workaround one could think of.
        // luckily android is smart enough to stop the cycle...
        findViewById(R.id.smallBar).requestLayout();
    }

    public void setBarHeight(float value, float max) {
        mBarHeight = value / max;
        View bar =   findViewById(R.id.smallBar);

        bar.requestLayout(); // because when we create this view onDraw is called...
        bar.invalidate();    // ...but not when we modify it!!!
                            //so we need to invalidate too
    }
//...
于 2010-11-12T19:37:37.717 回答