在android中的视图下画线

发布于 2024-12-05 17:30:47 字数 459 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人心善变 2024-12-12 17:30:47

你正试图重新发明轮子。 Z 排序已在窗口管理子系统中实现,您可以使用它。

  1. 创建您想要绘制的自定义视图。
  2. 使用 android:clickable="false"setClickable(false) 使其不可点击。
  3. 使其背景透明并实现dispatchDraw()
  4. 将所有不想绘制的视图放在视图层次结构中的该视图之上。

You're trying to reinvent the wheel. Z-ordering is already implemented in the window management subsystem and you can use it.

  1. Create a custom view you want to draw on.
  2. Make it non-clickable using android:clickable="false" or setClickable(false).
  3. Make its background transparent and implement dispatchDraw().
  4. Put all the views you don't want to draw on above this view in the view hierarchy.
夏末染殇 2024-12-12 17:30:47

绘制线条后调用 super.dispatchDraw() 。 viewgroup 调用dispatchDraw来绘制其子项,因此在您的情况下,调用super.dispatchDraw()将首先绘制按钮,然后在其上绘制线条。这样做 dispatchDraw

更新代码

class myListViewWithLine extends ListView {
....
    @Override
    protected void dispatchDraw(Canvas canvas) {

         Paint p = new Paint();
         p.setColor(Color.Black);
         canvas.drawLine(0,0,100,100,p);

         super.dispatchDraw(canvas);
    }
....
}

Call super.dispatchDraw() after drawing the line. The dispatchDraw is called by viewgroup for drawing its children, so in your case, calling super.dispatchDraw() will draw the button first then you are drawing the line over it. Do dispatchDraw this way :

Updated code

class myListViewWithLine extends ListView {
....
    @Override
    protected void dispatchDraw(Canvas canvas) {

         Paint p = new Paint();
         p.setColor(Color.Black);
         canvas.drawLine(0,0,100,100,p);

         super.dispatchDraw(canvas);
    }
....
}
甜尕妞 2024-12-12 17:30:47

另一种方法是在按钮下添加一个具有 1 个倾角作为高度和背景颜色的视图。它应该看起来像一条线。

View v = new View(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,1);
v.setLayoutParams(lp);
v.setBackgroundColor(Color.WHITE);

addView(v,INDEX NUMBER AFTER THE BUTTON);

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.

View v = new View(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,1);
v.setLayoutParams(lp);
v.setBackgroundColor(Color.WHITE);

addView(v,INDEX NUMBER AFTER THE BUTTON);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文