2016-09-18 57 views
0

线我建立从this answer定制的EditText,这是输出我得到,自定义EditText上没有显示上述

enter image description here 问题 行不会在右上角开始,他们在开始的地方中间,可能是错的。

下面是代码,

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> 

回答

0

我所做的加入重力和一切都很好,

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