Android - canvas onDraw 不触发

发布于 2024-12-08 22:40:33 字数 1452 浏览 0 评论 0原文

我正在尝试将画布视图添加到水平滚动视图内的相对布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#fff"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/hsv"
        android:layout_width="fill_parent"
        android:background="#EEDB00"
        android:layout_height="30mm">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/canvas"
            android:background="#000"
            android:layout_width="300mm"
            android:layout_height="20mm">
        </RelativeLayout>

    </HorizontalScrollView>
</RelativeLayout>

我创建了一个名为 CanvasView 的类,它扩展了 View,并且通过重写 onDraw() 绘制了一些基本形状。但是,当我这样做时,画布不会出现在相对布局中:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        cView = new CanvasView(this);
        rLayout = (RelativeLayout)this.findViewById(R.id.canvas);
        rLayout.addView(cView);
    }

但是,当我通过调用 setContentView(cView); 直接添加它时,它可以工作。在挖掘过程中,我发现当我调用 addView() 时,onDraw() 根本没有触发,因此画布没有被绘制......关于如何解决这个问题?

I am trying to add a canvas view to a relative layout inside a horizontal scroll view.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#fff"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/hsv"
        android:layout_width="fill_parent"
        android:background="#EEDB00"
        android:layout_height="30mm">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/canvas"
            android:background="#000"
            android:layout_width="300mm"
            android:layout_height="20mm">
        </RelativeLayout>

    </HorizontalScrollView>
</RelativeLayout>

I created a class called CanvasView which extends View, and I drew some basic shapes by over-riding onDraw(). However the canvas does not appear in the relativelayout, when I do:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        cView = new CanvasView(this);
        rLayout = (RelativeLayout)this.findViewById(R.id.canvas);
        rLayout.addView(cView);
    }

However, when I directly add it by calling setContentView(cView); it works. On digging, I found that when I call addView(), the onDraw() is not firing at all, and hence the canvas is not drawn... Any ideas on how to fix this?

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

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

发布评论

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

评论(1

沒落の蓅哖 2024-12-15 22:40:33

我目前无法自己测试这一点,但我认为您的问题是您没有将任何 LayoutParams 应用于视图,这可能意味着它不占用屏幕空间。如果视图曾经离开屏幕或完全被遮挡,或者系统决定它绘制的任何内容都不可见,那么我相信 onDraw() 根本不会被调用。

将视图添加到相对布局时,尝试为视图设置一些宽度和高度:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50,50);
rLayout.addView(cView, params);    

I'm not in a position to test this myself at the moment, but I think your problem is that you're not applying any LayoutParams to the View, which may mean it occupies no screen space. If a View is ever off-screen or completely obscured or the system otherwise decides that nothing it draws will be visible, then I believe onDraw() won't be called at all.

Try setting some width and height to your View when you add it to your RelativeLayout:

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