【问题标题】:Android onDraw doesn't draw anything in custom EditTextAndroid onDraw 不会在自定义 EditText 中绘制任何内容
【发布时间】:2017-07-27 06:50:37
【问题描述】:

similar problems

我有一个自定义编辑文本:

public class MyEditText extends AppCompatEditText {

public MyEditText(Context context) {
    super(context);
}

public MyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, getPaint());
}

}

在xml中:

   <com.angcyo.myapplication.MyEditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

正常效果:

effect

但如果你同时设置 android:gravity="right" android:inputType="text"

像这样:

<com.angcyo.myapplication.MyEditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:gravity="right"
    android:inputType="text"/>

嗯,结果是……

result

发生了什么?什么都不画?

【问题讨论】:

    标签: android-edittext ondraw


    【解决方案1】:

    尝试扩展 EditText 而不是 AppCompatEditText。 并把getmeasuredHeight()和getmeasuredWidth()改成getHeight()和getWidth()再看。

    初始化画图 油漆油漆=新油漆();油漆.setColor(颜色。黑色); 并在第三个参数中将半径更改为50f或60f,然后查看。

    【讨论】:

    • @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);油漆油漆 = 新油漆();油漆.setColor(颜色。黑色); canvas.drawCircle(getWidth() / 2, getHeight() / 2, 50f, 油漆); } 没有效果...天哪
    • 你能发布完整的xml吗?
    • schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:showIn="@layout/ activity_main">
    猜你喜欢
    • 2016-02-23
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多