问题描述
我做了一个GraphBar自定义视图是一个 RelativeLayout的
与的TextView
在底部,一个的ImageView
,不同的高度,上面。这里是code:
I made a "GraphBar" custom view that is a RelativeLayout
with a TextView
on the bottom and an ImageView
, varying in height, above that. Here is the code:
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
?任何帮助吗?我想无处不在,并与无效()
了。
While adding these GraphBars and setting their height in onCreate()
works well, if I create them onClickSomething or call again setBarHeight()
on the created bars, the only way to see changes is Load View Hierarchy. I've been told here that it means I need to call somewhere requestLayout()
. Where else than after modifying mBarHeight
? Any help? I tried everywhere, and with invalidate()
too.
感谢您,安德烈
(如果你需要我可以张贴活动与我做我的测试和graphbar.xml)
(if you need I can post the activity with which i do my tests and the graphbar.xml)
我发现它可能错误。解决方法应该是,再次呼吁 requestLayout()
。我还是不明白的地方我可以把通话。
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.
推荐答案
我终于找到了一种方法再次调用 requestLayout()
。我叫 setWillNotDraw(假)
的构造函数,以便在的OnDraw()
(即在 onLayout()
)我可以调用额外的 requestLayout()
。这会产生一个愚蠢的周期,但在美学解决了这个问题。
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.
如果有人知道一个更好的解决方案,让我知道... 在这里新的code(修改毗邻评论):
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
}
//...
这篇关于当添加的onCreate我的自定义视图只得到画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!