【问题标题】:Custom EditText does not show lines above自定义 EditText 不显示上面的行
【发布时间】:2016-09-18 11:22:23
【问题描述】:

我正在从this answer 构建一个自定义 EditText,这是我得到的输出,

问题 这些线不是从顶部开始,而是从中间的某个地方开始,可能是什么问题。

这是代码,

public class LinedEditText extends EditText {

    private static Paint linePaint;

    static {
        linePaint = new Paint();
        linePaint.setColor(Color.BLACK);
        linePaint.setStyle(Paint.Style.STROKE);
    }

    public LinedEditText(Context context, AttributeSet attributes) {
        super(context, attributes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect bounds = new Rect();
        int firstLineY = getLineBounds(0, bounds);
        int lineHeight = getLineHeight();
        int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);

        for (int i = 0; i < totalLines; i++) {
            int lineY = firstLineY + i * lineHeight;
            canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
        }

        super.onDraw(canvas);
    }
}

这是我的 XML,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/PeachPuff"
    >

    <TextView
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:padding="8dp"
        android:layout_margin="10dp"
        android:background="@drawable/shape"
        android:layout_gravity="center_horizontal" />



    <com.random.simplenotes.LinedEditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />


</LinearLayout>

【问题讨论】:

    标签: android android-edittext android-custom-view


    【解决方案1】:

    我所做的只是增加了重力,一切都很好,

    <com.random.simplenotes.LinedEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textMultiLine"
            android:gravity="top|left"
            />
    

    【讨论】:

    • 感谢您的回答。它节省了我的时间。它就像一个魅力
    猜你喜欢
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多