Android 重写 onDraw() 问题

发布于 2024-11-10 06:29:20 字数 1614 浏览 0 评论 0原文

我这里有一个小问题是我到目前为止的代码:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        setContentView(new GraphicView(this));

        TextView mainLabel = (TextView)findViewById(R.id.title);
            mainLabel.setText("Android Circle Path");
    }

    static class GraphicView extends View{
        public GraphicView(Context context){
            super(context);
        }

        @Override
        public void onDraw(Canvas canvas){


        }
    }  
}

和 main.xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip">

        <TextView
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dip"
            android:textSize="24.5sp"
            android:id="@+id/title">
        </TextView>

    </LinearLayout>
</LinearLayout>

当我尝试在 onDraw() 函数中设置文本时,应用程序崩溃了,我做错了什么?

I have a small issue here is the code i have so far:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        setContentView(new GraphicView(this));

        TextView mainLabel = (TextView)findViewById(R.id.title);
            mainLabel.setText("Android Circle Path");
    }

    static class GraphicView extends View{
        public GraphicView(Context context){
            super(context);
        }

        @Override
        public void onDraw(Canvas canvas){


        }
    }  
}

And the main.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip">

        <TextView
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dip"
            android:textSize="24.5sp"
            android:id="@+id/title">
        </TextView>

    </LinearLayout>
</LinearLayout>

When i try to set the text in the onDraw() function the app crashes, what am i doing wrong?

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

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

发布评论

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

评论(2

雨轻弹 2024-11-17 06:29:20

视图的 onDraw 方法应该用于单独渲染到该视图。您不应该在另一个视图中调用 UI 更新方法。将用于设置标题的代码移至 Activity 的 onCreate 方法中,因为只需执行一次。然后只需在自定义视图中绘制圆圈即可。另外,由于您的自定义视图未实现 ViewParent,因此不应使用它来保存标题 TextView。重新组织 xml 以将标题视图移到 GraphicsView 之外

A view's onDraw method should be used to render to that view alone. You should not be calling UI update methods in another view. Move the code for setting the title into your activity's onCreate method, since that only needs to be done once. Then just draw your circle in your custom view. Also, since your custom view does not implement ViewParent, it shouldn't be used to hold the title TextView. Reorganize your xml to move the title view outside the GraphicsView

有木有妳兜一样 2024-11-17 06:29:20

TextView 是在该 ActivityonCreate 方法中创建的局部变量。因此,TextView 的范围仅限于 onCreate 方法内。您无法在该方法之外访问它。此外,您尝试做的事情不是正确的方法,在 GraphView 的 onDraw 方法中,您应该单独绘制 GraphView 组件,不是外面的景色或其他任何东西。

TextView is a local variable created in the onCreate method of that Activity. So the scope of TextView is confined within that onCreate method only. You can't access it outside that method. Moreover what you are trying to do is not the right way, inside GraphView's onDraw method you are supposed to draw the GraphView components alone, not the outside views or anything else.

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