2017-10-02 11 views
0

il a essayé et utiliser CustomTextView, mais tout le texte s'affiche en une seule ligne. je veux montrer le texte dans plusieurs lignes.Comment afficher du texte sur CustomTextView dans plusieurs lignes

ici mon code

<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" /> 

je minLines, maxLines, singleLine="false", inputType="textMultiLine" mais toujours montrer comme ceci:

enter image description here

classe ici mon CustomTextView j'ai cacher une partie de mon code cette le code affiche également le texte sur une seule ligne.

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()); 
       } 
} 
+1

affichez votre classe textview personnalisée –

+0

Ce n'est pas votre XML qui cause le problème. Avec un 'TextView' non modifié et votre XML dans un 'RelativeLayout', j'obtiens quatre lignes (dans le coin supérieur gauche, cependant). – kalabalik

+1

Pour que nous puissions vous aider, vous devez ajouter votre fichier XML complet et le code de CustomTextView. – Frank

Répondre

0

i supprimer tout x_position et 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

0

Utilisez

android:maxLength="10"
Comme l'exigence de longueur

le long des lignes max

+0

il montre seulement dix caractères non nouvelle ligne. – Attaullah

+0

mec après dix caractères une nouvelle ligne sera forme ..... et le nombre 10 est variable, vous pouvez définir autant que vous pouvez –

+0

ne fonctionne pas afficher une seule ligne – Attaullah