2017-07-20 3 views
1

私は、テキストビュー内で9つのパッチとワープロを処理しようとしています。問題はTextViewがラップされたテキストの幅をリサイズしないため、チャットバブルが大幅に縮小されてしまいます。アンドロイドのサイズ変更チャットバンプの幅をラップされたテキスト

チャットギャップ

image

ViewHierarchyの調査に気泡これらのギャップは、TextViewに、チャットレイアウトのビュー階層内にあることを示しています。 9パッチには関係しません。 image

は、誰かが...あなたは、自動的にコンテンツに対応するためにサイズ変更したビットマップ画像を作成することができます。レイアウトの背景として

<LinearLayout 
android:id="@+id/messages" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" 
android:orientation="vertical"> 

<TextView 
    android:id="@+id/senderName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="marquee" 
    android:gravity="bottom|start" 
    android:maxLines="1" 
    android:layout_marginTop="5dp" 
    android:layout_marginStart="18dp" 
    android:layout_marginLeft="18dp" 
    android:textColor="@color/talkatone_gray" 
    android:textSize="@dimen/chat_item_meta_info_text_size" 
    android:visibility="gone"/> 

    <TextView 
     android:id="@+id/message_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end" 
     android:layout_marginBottom="@dimen/chat_bubble_margin_top" 
     android:background="@android:color/transparent" 
     android:lineSpacingExtra="4sp" 
     android:clickable="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:gravity="center_vertical" 
     android:text="@string/form_chat_failed_to_open" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textColor="@color/chat_text" 
     android:textSize="@dimen/chat_text_message_size"/> 

    <FrameLayout 
     android:id="@+id/attachment_frame" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="@dimen/chat_bubble_margin_top" 
     android:orientation="vertical"> 

      <ImageButton 
       android:id="@+id/video_play_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical|center_horizontal" 
       android:background="@android:color/transparent" 
       android:contentDescription="@null" 
       android:src="@drawable/play_gif"/> 
    </FrameLayout> 

+0

wender_contentと同じwender_contentを送信者名と同じ –

+0

@OussemaArouaがあなたの提案を申し入れましたが、問題は解決していません。 – efuntikov

+0

これは、特定の行の最後の単語が左スペースに収まりきらないためです右に行くので、それは下がる。小さな単語で試してみて、一度確認してください。 – Debanjan

答えて

0

主な問題は、あなたがviewholderパターンの実装とアダプタでそれを使用することです。

だから、あなたが解決する必要のある2つの問題があります:あなたはそれを再利用します前に、

  1. がTextViewの幅を毎回リセットします。
  2. TextViewメジャーで最も長い行を定義し、適切な幅を設定します。リセットTextViewの幅については

あなたのホルダーを再利用し、アダプタgetViewメソッド()メソッド、次に追加する:あなたはTextViewにから拡張カスタムビューを作成し、onDrawオーバーライドする必要がある第二の問題について

ViewGroup.LayoutParams paramsReset = holder.messageText.getLayoutParams(); 
paramsReset.width = ViewGroup.LayoutParams.WRAP_CONTENT; 
holder.messageText.setLayoutParams(paramsReset); 

()メソッドは、リストビューで使用することのために必要なのあなたはホルダーがビューに登場しているとき、呼び出して他のコールバックを、見つける必要がありますよう

@Override 
protected void onDraw(Canvas canvas) 
{ 
    super.onDraw(canvas); 
    if(getLineCount() > 1) 
    { 
     float longestLineWidth = -1; 
     for (int lineIndex = 0; lineIndex < getLineCount(); lineIndex++) 
     { 
      int lineStartIndex = getLayout().getLineStart(lineIndex); 
      int lineEndIndex = getLayout().getLineEnd(lineIndex); 
      String currentTextLine = getText().toString().substring(lineStartIndex, lineEndIndex); 
      // Added "_____" for your paddings. 
      float currentLineWidth = getPaint().measureText(currentTextLine + "_____"); 

      if (longestLineWidth < currentLineWidth) 
      { 
       longestLineWidth = currentLineWidth; 
      } 
     } 
     ViewGroup.LayoutParams paramsNew = getLayoutParams(); 
     paramsNew.width = (int) longestLineWidth; 
     setLayoutParams(paramsNew); 
    } 
} 

私はonDrawを(上書き)も、見えるので。


これは機能しますが効率的ではなく、その他の問題があります。これを最初の手順として使用してください。

0

使用9パッチ画像を、同様の問題に直面する可能性がありビューの

0

残念ながら、あなたの質問にも答えが含まれています

TextViewには、あなたがwrap_contentを使用して複数行を許可すると、システムが最初に成長します包まれたテキスト

にその幅を変更しません。 TextViewの幅をできるだけ広くしてから、改行の追加を開始してください。利用可能な幅を取り戻すために、最後には決して通過しません。

もちろん、すべてのことと同様に、独自のカスタムTextViewクラスを作成することでこの問題を解決できます。ここに、同様の問題を解決しようとした別のプロジェクトへのリンクがあります。多分それはあなたにアイデアを与えるのに役立ちます。

その他の質問:Uniform text wrapping in TextView

カスタムのTextView libに:あなたのケースでhttps://github.com/dziobas/UniformTextView

+0

ありがとう!それをアカウントに入れます – efuntikov

関連する問題