2016-05-26 16 views
0

私はTextViewに複数行あります。ビューがそこに収まるかどうかを知るために、最終的にスペースをどのように決定するのですか?TextView - ビューを最後に移動

例:

-------------------- 
| I am a multiline | 
| text that has | 
| space at the  | 
| end.  <View>| 
-------------------- 

-------------------- 
| I am a multiline | 
| text that has | 
| not enough space | 
| at theeee end. | 
|   <View>| 
-------------------- 

だから私は十分なスペース

+3

かなり曖昧ですが、文字が収まる残りのスペースやdpの実際の測定値をテキストからTextViewの範囲にしたいですか? – mastrgamr

+0

スペースはpxまたはdpで、文字ではない – shilch

+0

テキストの終わりまでのスペースを計算したいのですか? – umerk44

答えて

1

コーディング楽しみます他のビューが残りの空間に収まるかどうかを計算します。

ここでは2つのテキストビューでサンプルを作成しましたが、希望のビューに適用できます。 xmlは非常にまっすぐ遠く、普通のものではありません。テキスト1とテキスト2のレイアウトにに描かれた後

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="kasjdf as df asd aasdfasdff as dfa sd sdf as df asd fa sd fa sdf as dfanas,df askdjfasjdf lkajsldkf jlaksjdfajsdkfj alskdj fklasjdflkasjdlkfdflk ajskldf" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="attached text" 
     android:textColor="@android:color/holo_blue_dark" /> 

</RelativeLayout> 

ここでキャッチ、私はテキスト2の幅と一緒にテキスト1の最後の行に達しないところ計算し、そしてだ - それが合うことができるかどうかを確認し、画面の幅。

  • text1とtext2を描画した後で、幅と右の位置を確認できるので、setText2Positionの両方でそれらを待っています。
  • ここでキャッチするのは、上記の計算に従ってtext2にルールを追加することです。あなたの意見に余白とパディングを持っている場合は
  • は、それを計算する必要があり、同様 FE、テキスト2は、パディングを持っている場合、チェックは次のようになります。

lastLineRight + text2.getWidth()+テキスト2。

bottom

:getPaddingRight()+ text2.getPaddingLeft()> screenWidth

public class MainActivity extends AppCompatActivity { 

    private boolean[] areBothViewsDrawn = {false, false}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final TextView text1 = (TextView) findViewById(R.id.text1); 
     final TextView text2 = (TextView) findViewById(R.id.text2); 

     text1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       text1.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       areBothViewsDrawn[0] = true; 
       setText2Position(text1, text2); 
      } 
     }); 

     text2.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       text2.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       areBothViewsDrawn[1] = true; 
       setText2Position(text1, text2); 
      } 
     }); 

    } 

    private void setText2Position(TextView text1, TextView text2) { 
     if (!areBothViewsDrawn[0] || !areBothViewsDrawn[1]) 
      return; 

     int lineCount = text1.getLayout().getLineCount(); 
     float lastLineRight = text1.getLayout().getLineRight(lineCount - 1); 

     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     int screenWidth = size.x; 

     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text2.getLayoutParams(); 
     if (lastLineRight + text2.getWidth() > screenWidth) { 
      //The width of the last line of text1 is too long for the text2 to fit, so text2 must be in another line 
      params.addRule(RelativeLayout.BELOW, text1.getId()); 
     } else { 
      //The width of the last line of text1 and text2 smaller then the application's width, so text2 can be in the same line as text1 
      params.addRule(RelativeLayout.ALIGN_BOTTOM, text1.getId()); 
     } 
    } 
} 

ここでは私のコードのためのいくつかのPOCのです

right

+0

これは素晴らしいソリューションですありがとうございます! – shilch

0

がある場合、私はこの質問を考えて、それがテキストを重ねるべきではない、私の見解は、右下隅になりたいが、それの右側より多くの文脈を必要とする。 (なぜあなたがこれをしたいのか不思議なだけです)

TextViewにXMLで定義された幅と高さが設定されていると、このようなことが起こりやすくなります。正確に情報を取得しているのであれば、正しい測定値が得られているかどうか確認するには、steps you'd have to takeがあります。

TextViewの幅全体を取得し、実際のテキストの幅(おそらくgetLineCount()/getLineBounds(int, Rect?)getTextScaleX()の組み合わせですか?)との違いを得ることが1つのアプローチです。次にconvert it to dp。これを達成するための

+0

私の更新された質問を参照 – shilch

0

使用フレームレイアウト......

これは

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:background="#aaa"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:maxLines="5" 
     android:minLines="3" 
     android:textSize="25dp" 
     android:text="This is multiline problem which is solve using the Frame Layout that has to mataintan the title in the end. " 
     android:textColor="#000" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|bottom" 
     android:text="By Sushildlh" 
     android:textColor="#000" 
     android:textSize="25dp" /> 

</FrameLayout> 

XMLファイルであり、これが出力されます: -

image

使用このためピクセル ....

float pixel=textView.getWidth();//this give value in pixels 
DPため

と、この......

float dp = getResources().getDisplayMetrics().density; 
dp = t1.getWidth()/dp; //its provide value in density pixels 

基本的に、あなたは最後の行の右(y軸がない場所を確認する必要があります......

+0

これは私が達成したいものではない、私は最高を望む – shilch

+0

あなたは何を意味するか高い? – sushildlh

+0

いいえ、多分タイトルが悪いです。スペースがあれば、TextViewの最後までビューを移動したい。私の質問を参照 – shilch

関連する問題