自定义view的drawable区域覆盖其他领域

发布于 2024-10-28 01:26:44 字数 2137 浏览 1 评论 0原文

我有一个自定义视图,有点像垂直进度条。它只是一个自定义视图,绘制在画布上以从下到上填充完成百分比。在我的 xml 中,我在自定义控件上方有文本,在自定义控件下方有文本(其中有两个,一个在左侧,一个在右侧,这是右侧):

    <RelativeLayout android:layout_width="150dp"
        android:layout_height="fill_parent" android:layout_alignParentRight="true">
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bar_right_text" android:text="Right"
android:layout_alignParentTop="true" android:gravity="center_horizontal" />
        <com.myApp.VBarView android:id="@+id/right_bar"
            android:layout_width="match_parent"
                    android:layout_height="match_parent"
                        android:layout_below="@id/bar_right_text"/>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bar_right_time_text" android:text="Time Remaining" android:layout_below="@id/right_bar" android:gravity="center_horizontal" />
    </RelativeLayout>

在我的自定义视图中,我以各种方式检查高度可以想到,但我总是得到一个一直到屏幕底部的高度

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    // h here is 489
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int myHt = getHeight();
    int myMeasHt = getMeasuredHeight();
    Rect canvasRct = canvas.getClipBounds();
    // all of these (myHt, myMeasHt, and canvasRct.getHeight()) are all 489
}

到目前为止,我已经尝试反转布局中的视图,所以顶部文本是第一个(顶部对齐),底部文本是第二个(底部对齐),自定义控件在最后(layout_above底部文本),同时有match_parent和wrap_content,都没有用。我尝试更改为线性布局,但仍然没有更好的结果。

在代码所示的情况下,显示了顶部文本,但底部文本消失了。当我更改顺序以使自定义视图位于底部文本之上的布局时,它只是将绘图区域向上移动,以便底部文本可见,但覆盖了顶部文本。请注意,当我说底部文本消失时,我的意思是消失,而不仅仅是被掩盖。我的最终测试是将布局中的layout_height硬编码为“350dp”。

在所有情况下,我在这个特定设备上总是得到 489 的高度,即使高度是硬编码的。我不知道我需要做什么才能让屏幕的中间部分成为我可以绘制的东西,而让其余部分保持不变。自定义视图是否被假定为布局中的最后一件事?我可以让它工作的唯一方法是更改​​顺序,以便自定义视图位于布局中的最后,以便自定义区域的底部位于底部文本的顶部,底部文本仍然显示,然后手动添加大约 150 到顶部(即不要在提供给 onDraw() 的画布上写入零),因此它不会覆盖顶部文本。幸运的是,我正在绘制的图形是半透明的,因此我可以看到文本是否被覆盖或刚刚消失。

有谁有更多关于它应该如何工作的信息?我是否设法使对我所做的事情的解释比实际问题更令人困惑?

谢谢, 国标

I have a custom view that is sort of a vertical progress bar. It's just a custom view, drawing onto the canvas to fill from bottom to top with the percent complete. In my xml, I have text above, and text below the custom control (there are two of these, one on the left, one on the right, here's the right):

    <RelativeLayout android:layout_width="150dp"
        android:layout_height="fill_parent" android:layout_alignParentRight="true">
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bar_right_text" android:text="Right"
android:layout_alignParentTop="true" android:gravity="center_horizontal" />
        <com.myApp.VBarView android:id="@+id/right_bar"
            android:layout_width="match_parent"
                    android:layout_height="match_parent"
                        android:layout_below="@id/bar_right_text"/>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bar_right_time_text" android:text="Time Remaining" android:layout_below="@id/right_bar" android:gravity="center_horizontal" />
    </RelativeLayout>

In my custom view, I check the height in every way I can think of, but I always get a height that goes all the way to the bottom of the screen

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    // h here is 489
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int myHt = getHeight();
    int myMeasHt = getMeasuredHeight();
    Rect canvasRct = canvas.getClipBounds();
    // all of these (myHt, myMeasHt, and canvasRct.getHeight()) are all 489
}

So far, I have tried reversing the views in the layout, so the top text is first (top aligned), the bottom text is second (bottom aligned), and the custom control is last (layout_above the bottom text), with both match_parent and wrap_content, to no avail. I tried changing to a linear layout, still with no better result.

In the case shown in code, the top text is displayed, but the bottom text is gone. When I changed the order so the custom view was layout_above the bottom text, it just moved the drawing area up so the bottom text was visible, but covered the top text. Note that when I say the bottom text is gone, I mean gone, not just covered up. My final test was to hardcode the layout_height to "350dp" in the layout.

In all cases, I always get a height of 489 on this particular device, even with the height hard-coded. I can not figure out what I need to do to get the middle part of the screen as something I can draw on and leave the rest alone. Are custom views assumed to be the last thing in a layout? The only way I can get this to work is to change the order so the custom view is last in the layout so that the bottom of the custom area is on top of the bottom text, the bottom text is still displayed, and then manually add about 150 to the top (i.e. don't write into zero on the canvas provided to the onDraw()) so it doesn't cover the top text. Fortunately, the graphic I'm drawing in is semi-transparent, so I can see whether the text is covered or just gone.

Does anyone have more info on how this is supposed to be working? Did I manage to make the explanation of what I did more confusing than the actual problem?

Thanks,
gb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

年少掌心 2024-11-04 01:26:44

有人建议离线时不要只将自定义字段放置在其他字段之一的“上方”或“下方”,而是同时执行这两个字段。将其保留为 XML 中的最后一项,并设置 android:layout_aboveandroid:layout_below。我这样做了,现在我的身高正确了。

Someone suggested offline that instead of just placing the custom field "above" or "below" one of the other fields, do both. Keep it as the last item in the XML, and set android:layout_above and android:layout_below. I did that, and now my heights come in correctly.

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