如何在编辑文本Android中显示水平线?

时间:2013-06-25 05:19:32

标签: android android-edittext

我想在我的android记事本应用程序中显示带衬里的编辑文本,我在Activity中创建了linededittext类。但是我无法在xml文件中为我们设置视图。它给予android.view.inflateException,二进制xml文件第17行膨胀类“

   
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<com.android.sassypuma.todo.task.Add_Task.LinedEditText
android:id="@+id/note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="5dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:capitalize="sentences"
/>
</LinearLayout>

我使用LinedEditText类的活动:

public class Add_Task extends Activity{

private EditText description;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_task);
    description = (EditText)findViewById(R.id.note);
}

}

LinedEditText活动:

class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;

// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public void init(){
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(0xFF668800); //SET YOUR OWN COLOR HERE
}
@Override
protected void onDraw(Canvas canvas) {
    //int count = getLineCount();

    int height = getHeight();
    int line_height = getLineHeight();

    int count = height / line_height;

    if (getLineCount() > count)
        count = getLineCount();//for long text with scrolling

    Rect r = mRect;
    Paint paint = mPaint;
    int baseline = getLineBounds(0, r);//first line

    for (int i = 0; i < count; i++) {

        canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
        baseline += getLineHeight();//next line
    }

    super.onDraw(canvas);
  } 
}

请建议。感谢...

3 个答案:

答案 0 :(得分:0)

您不使用view作为标记名。您使用完全限定的类名

答案 1 :(得分:0)

在您的活动中,您可以使用自定义名称调用自定义的EditText,如下所示。

public class Add_Task extends Activity{

LinedEditText description;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_task);
    description = (LinedEditText)findViewById(R.id.note);
}
}

这样它才会起作用

答案 2 :(得分:0)

亲爱的兄弟,从LinedEditText类删除构造函数的第三个参数