Android 重写 onDraw() 问题
我这里有一个小问题是我到目前为止的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图的
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'sonCreate
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 GraphicsViewTextView
是在该Activity
的onCreate
方法中创建的局部变量。因此,TextView
的范围仅限于onCreate
方法内。您无法在该方法之外访问它。此外,您尝试做的事情不是正确的方法,在 GraphView 的 onDraw 方法中,您应该单独绘制 GraphView 组件,不是外面的景色或其他任何东西。TextView
is a local variable created in theonCreate
method of thatActivity
. So the scope ofTextView
is confined within thatonCreate
method only. You can't access it outside that method. Moreover what you are trying to do is not the right way, insideGraphView
'sonDraw
method you are supposed to draw theGraphView
components alone, not the outside views or anything else.