8

2つのレイアウトを等幅のの縦方向に作成しました。また、文字列データをテキストビューに動的に表示するようにしました。 stringがレイアウトの幅より大きい場合、stringはレイアウトの幅にラップされ、残りの文字列に対しては新しいTVを動的に作成します。残りの文字列が終了するまで、このプロセスは終了します。次の文字列については、同じプロセスが続行されます。プロセスがlinearlayout1のボトムに達すると、残りの文字列はlinearlayout2から開始されます。そしてプロセスはlinearlayout2の底に達するまで続きます。レイアウトの幅に応じて文字列を表示する方法

私はこれは、このヘルプあなたかもしれこの

private void nextlinechar(int numChars,String devstr) { 
    nextchar=devstr; 
    Log.d("char 1",""+nextchar); 
    TextView sub=new TextView(getApplicationContext()); 
    sub.setLines(1); 
    sub.setTextColor(Color.BLACK); 
    sub.setTextSize(textsize); 
    sub.setText(nextchar); 
    nextchar=devstr.substring(nextcharstart); 
    String textToBeSplit = nextchar; // Text you want to split between TextViews 
    String data=TextMeasure(nextchar,sub); 
    float myTextSize=sub.getTextSize(); 
    float textView2Width=400; 

// String next=TextMeasure(nextchar,sub); 
    Paint paint = new Paint();  
    paint.setTextSize(myTextSize); // Your text size 
    numChars1= paint.breakText(textToBeSplit, true,textView2Width, null); 
    nextchar1=nextchar.substring(numChars1); 
// Log.d("char",""+i+" "+nextchar.length()); 
    main.addView(sub); 
    nextlinechar(numChars1,nextchar); 
} 

イラスト

enter image description here

+1

これまでに試したコードをお見せください。 – Piovezan

+0

@Piovezanちょっと私の編集した質問 –

+0

非常に複雑な質問、テキストの壁を理解してください。代わりに、あなたが必要としているものと今見ているものを図式的に説明できますか? – Siddharth

答えて

0

Refferのように試してみました:

package com.example.demo; 

    import android.content.Context; 
    import android.graphics.Paint; 
    import android.util.AttributeSet; 
    import android.util.TypedValue; 
    import android.widget.TextView; 

    public class FontFitTextView extends TextView { 

     public FontFitTextView(Context context) { 
      super(context); 
      initialise(); 
     } 

     public FontFitTextView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      initialise(); 
     } 

     private void initialise() { 
      mTestPaint = new Paint(); 
      mTestPaint.set(this.getPaint()); 
      // max size defaults to the initially specified text size unless it is 
      // too small 
     } 

     /* 
     * Re size the font so the specified text fits in the text box assuming the 
     * text box is the specified width. 
     */ 
     private void refitText(String text, int textWidth) { 
      if (textWidth <= 0) 
       return; 
      int targetWidth = textWidth - this.getPaddingLeft() 
        - this.getPaddingRight(); 
      float hi = 100; 
      float lo = 2; 
      final float threshold = 0.5f; // How close we have to be 

      mTestPaint.set(this.getPaint()); 

      while ((hi - lo) > threshold) { 
       float size = (hi + lo)/2; 
       mTestPaint.setTextSize(size); 
       if (mTestPaint.measureText(text) >= targetWidth) 
        hi = size; // too big 
       else 
        lo = size; // too small 
      } 
      // Use lo so that we undershoot rather than overshoot 
      this.setTextSize(TypedValue.COMPLEX_UNIT_PX, lo); 
     } 

     @Override 
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
      int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
      int height = getMeasuredHeight(); 
      refitText(this.getText().toString(), parentWidth); 
      this.setMeasuredDimension(parentWidth, height); 
     } 

     @Override 
     protected void onTextChanged(final CharSequence text, final int start, 
       final int before, final int after) { 
      refitText(text.toString(), this.getWidth()); 
     } 

     @Override 
     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      if (w != oldw) { 
       refitText(this.getText().toString(), w); 
      } 
     } 

     // Attributes 
     private Paint mTestPaint; 
    } 
1

考えられる解決策1

FlowLayoutが必要です(here)。基本的に、テキストは右にオーバーフローする代わりにラップアラウンドする必要があります。

考えられる解決策2

代わりのWebViewを使用してみてください、そして2 webviewsのテキストを取り込みます。それはバグとしてではなく、より少ないコードでより速くなります。

私はそれぞれの単語を別々にクリックする必要があるときだけ、FlowLayoutを使用しました。基本的に、人々が文章のパーツオブパーフェクトを選択する文法のテスト。そのために私は各単語にリスナーが必要でした。

+0

b/wテキストで画像を追加するにはどうしたらいいですか? –

+0

ソリューション2は、あなたのコンテンツをより多くのことを楽しむことができるという点で最も優れています。 – Siddharth

0

次は私が(もちろん私は最初何が必要ありませんTextViweの組み込みメソッドを検索しようとしたが、いずれかを見つけることができませんでした)を思い付いたアイデアです:

  1. 画面サイズ(this.getResources()。getDefaultDisplay()で正しいメソッドを検索する)、必要なテキストサイズ、およびその間のスペースに応じて、各レイアウト(左および右)に表示する行数を決定します。レイアウト、およびテキストと上端/下端の間に配置されます。ほとんどのAndroidサイズのメジャーメソッドはサイズをピクセル単位で返すので、ピクセルをディップに変換する必要があるかもしれないし、逆もまた同じであることを忘れないでください。

  2. 各テレビ(tv.setLines())に最大行数を設定します。

  3. テキスト全体を左のテレビに設定します。

  4. ここで、テキストの最初の半分が左のテレビに表示されます。テレビの最大行数に達しているため、テキストの他の部分は非表示になります。それで、getText()を使って現在テレビに表示されているテキストを受け取ります(うまくいけば、テキストの最初の部分だけが返されます)。まだ試してみません、ごめんなさい、もしかしたら、いくつかのgetDisplayedTextか何か)。これで、単純なJavaメソッドを使用して、元のテキスト(部分文字列)からテキストの残りの部分だけを取り出して、2番目のTVに設定できます。

希望します。

関連する問題