如何在多行显示CustomTextView上的文本

时间:2017-10-02 08:01:26

标签: android textview

它尝试并使用CustomTextView,但所有文本都以单行显示。我想以多行显示文字。

这里是我的代码

<com.textdesign.views.CustomTextView
            android:id="@+id/customTextview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:singleLine="false"
            android:inputType="textMultiLine"
            android:maxLines="40"
            android:lines="20"
            android:minLines="5"
            android:text="this is sample text for multi-lines\nthis is sample text for multi-lines\nthis is sample text for multi-lines\nthis is sample text for multi-lines"
            android:textStyle="bold" />

我使用了minLinesmaxLinessingleLine="false"inputType="textMultiLine",但仍然显示如下:

enter image description here

这里我的CustomTextView类我隐藏了我的一些代码,这段代码也以单行显示文字。

public class CustomTextView extends AppCompatTextView {

    //Shadow Variable
    public static int shadow_length = 30; 

    public int x_direction = 1;
    public int y_direction = 1; 
    boolean shadow_Enable = false; 
    int color = Color.BLACK;
    float[] hsv = new float[]{0, 0, 0};  
    int getcol;
    Paint paint;
    Paint paint1;
    Paint paint2;   

    public CustomTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet,android.R.attr.textViewStyle);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(getTextSize());
    }

    @Override
    protected void onDraw(Canvas canvas) {   
            getPaint().setMaskFilter(null);
        TextPaint textPaint = getPaint();
        float x_position = (getWidth() - getPaint().measureText(getText().toString())) / 2f;
        float y_position = (int) ((getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));

        getPaint().setColor(shadowColor);

        //Center point for transformation
        PointF center_Point = new PointF(getWidth() / 2f, getHeight() / 2f);
        Camera camera = new Camera(); 

                canvas.drawText(getText().toString(), x_position, y_position, getPaint());
                }
}

2 个答案:

答案 0 :(得分:0)

使用

&#13;
&#13;
android:maxLength="10"
&#13;
&#13;
&#13;  根据长度要求

以及最大线

答案 1 :(得分:0)

我只删除x_position和y_position,

 @Override
    protected void onDraw(Canvas canvas) { 

     //.........
     StaticLayout mTextLayout = new StaticLayout(getText().toString(), getPaint(), canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
                    canvas.save();
     float textHeight = getTextHeight(getText().toString(), textPaint);
     int numberOfTextLines = mTextLayout.getLineCount();
     float textYCoordinate = mTextBounds.exactCenterY() - ((numberOfTextLines * textHeight) / 2);
     // text will be drawn from left
     float textXCoordinate = mTextBounds.left;
     canvas.translate(0, 0);
     // draws static layout on canvas
     mTextLayout.draw(canvas);
     canvas.restore();
     //..... 
}

enter image description here