在android中的视图下画线
我正在使用 onDispatchDraw(Canvas canvas) 方法在我的视图中绘制线条。当我调用 canvas.drawLine() 时,它总是在我的所有视图之上绘制线条。有没有办法使用 canvas.drawLine() 在按钮下方但在布局中的另一个视图之上绘制线条?
我已尝试以下操作,但它仍然在按钮上画线。
Button b;
RelativeLayout r;
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Paint p = new Paint();
p.setColor(Color.Black);
canvas.drawLine(0,0,100,100,p);
r.removeView(b);
r.addView(b);
}
I am using the onDispatchDraw(Canvas canvas) method to draw lines in my view. When I call canvas.drawLine() it always draws the line on top of all my views. Is there any way to draw the line under a button but on top of another view in my layout using canvas.drawLine()?
I have tried the following but it still draws the line over the button.
Button b;
RelativeLayout r;
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Paint p = new Paint();
p.setColor(Color.Black);
canvas.drawLine(0,0,100,100,p);
r.removeView(b);
r.addView(b);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你正试图重新发明轮子。 Z 排序已在窗口管理子系统中实现,您可以使用它。
android:clickable="false"
或setClickable(false)
使其不可点击。dispatchDraw()
。You're trying to reinvent the wheel. Z-ordering is already implemented in the window management subsystem and you can use it.
android:clickable="false"
orsetClickable(false)
.dispatchDraw()
.绘制线条后调用 super.dispatchDraw() 。 viewgroup 调用dispatchDraw来绘制其子项,因此在您的情况下,调用super.dispatchDraw()将首先绘制按钮,然后在其上绘制线条。这样做
dispatchDraw
:更新代码
Call
super.dispatchDraw()
after drawing the line. The dispatchDraw is called byviewgroup
for drawing its children, so in your case, callingsuper.dispatchDraw()
will draw the button first then you are drawing the line over it. DodispatchDraw
this way :Updated code
另一种方法是在按钮下添加一个具有 1 个倾角作为高度和背景颜色的视图。它应该看起来像一条线。
Another method will be to just add a view with 1 dip as height and background color whatever you like under the button. It should look like like a line.