2017-10-02 25 views
0

CustomTextViewを試してみましたが、すべてのテキストが1行で表示されます。私は複数の行にテキストを表示したい。複数行の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クラスを私は私のコードの一部にこれを隠していますコードは1行でテキストも表示します。

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

あなたのカスタムテキストビュークラスを表示します –

+0

あなたのXMLはこの問題の原因ではありません。変更されていない「TextView」とあなたのXMLを「RelativeLayout」の中に入れて、私は4行(左上隅にあります)を取得します。 – kalabalik

+1

私たちのお手伝いをするには、完全なXMLファイルとCustomTextViewのコードを追加する必要があります。 – Frank

答えて

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

0

使用

android:maxLength="10"
長さの要件

を1として、最大ライン

+0

新しい行ではなく10文字しか表示されません。 – Attaullah

+0

10文字後に新しい行が書かれ.....そして数字10は可変であり、できるだけ多く設定することができます –

+0

作業していないと1行しか表示されません – Attaullah

関連する問題