【发布时间】:2012-04-11 00:39:12
【问题描述】:
我正在尝试在我的 EditText 中实现自定义 onDraw() 方法。
onDraw 被调用 - 我可以看到日志消息,但它没有绘制任何内容。
谁能告诉我我做错了什么?
这是我的布局的摘录:
<view xmlns:android="http://schemas.android.com/apk/res/android"
class ="my.package.NotePadEditView"
android:inputType="textMultiLine"
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="@android:color/transparent"
android:singleLine="false"
>
<requestFocus/>
</view>
</ScrollView>
这是类(现在只是一些测试代码):
public class NotePadEditView extends EditText {
Paint paint = new Paint();
public NotePadEditView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(0xFF0000);
}
@Override
protected void onDraw(Canvas canvas) {
Log.d("NotePadEditView", "Calling onDraw()"); // These log messages are displaying
canvas.drawLine(0, 0, 50, 50, paint); // just some random stuff so we know when we are done. (Note: these are not displaying - what's up with that???)
canvas.drawText("Hello, World", 30, 30, paint);
super.onDraw(canvas);
}
// more constructors, etc
【问题讨论】:
-
尝试先致电
super.onDraw(canvas);。也许那是在您的drawLine()和drawText()之上。 -
我试过了——事实上我把整个绘图的东西放在一个方法中,并在 super.onDraw 之前和之后调用它——疯狂的困惑!
标签: android android-edittext android-view android-custom-view