5

TextViewにテキストを動的に表示したい。テキストはサーバーから動的に取得されます。これは、単一の単語または単一の行または段落であってもよい。テキストは、顧客の要求に基づいて56spというサイズのビューを表示しています。Android: - 単語の改行でハイフン " - "を追加する方法アンドロイド6.0以下のTextView

私の問題は、アプリケーションが大きなサイズのテキストを表示していることです。行末の単語区切りの場合、OSはマシュマロデバイスの下にハイフン( " - ")を自動的に表示しません。

例:テキスト: "キャリーオーバーのデータが利用可能になりまし" それは今AVA

ilable

私はこれを表示したい

キャリーオーバー

データとしてUIに表示されますas

キャリーオーバー

データ今すぐava-

ilableです。

しかし、これはマシュマロまたはそれ以上のデバイスで正しく機能しています。

のTextViewプロパティが、私はこの問題を解決する別の方法を実装

<TextView 
    android:id="@+id/tv_primary_headline" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:fontFamily="sans-serif-black" 
        android:lineSpacingExtra="@dimen/promo_primarytext_line_spacing" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="@color/navigation_selection_color" 
    android:textSize="@dimen/promo_primary_headline_size" 
    android:textStyle="bold" 
    android:visibility="visible" /> 

TextView mTvPrimaryHeadline = (TextView) view. 
        findViewById(R.id.tv_primary_headline); 
    this.mTvPrimaryHeadline.setText(Html.fromHtml(title)); 
+1

可能な複製(http://stackoverflow.com/questions/4454911/hyphenation-in-android) – rds

+0

は、それが表示する任意のオプションです。 Soft-Hyphenを試しましたが、動作しません。 GitHubライブラリの実装は非常に混乱しており、勝っています。ライセンスはありません。可能であれば、コードを共有してください。これは私の開発に役立ちます。 – Nithinjith

+0

私の実装では、string.xmlから文字列を取得することができません。値はJSONレスポンスから来ています。だから、オーバーフロー単語の場合には、Javaレベルのハイフネーションを作成することは可能ですか? – Nithinjith

答えて

1

以下の通りです。

すべてのデバイスの実装を一般化するには、文中の最も長い単語に基づいてテキストを動的に配置します。以下の2つの方法を使用して、完全な文章を渡してTextViewを使用してください。これにより、すべての画面のすべてのデバイスのテキストが自動的に配置されます。 " - " は、以下の6デバイスで[Androidの中ハイフネーション]の

/** 
    * 
    * @param message - Raw Header message from Server - Sentance/ Paragraph. 
    *    The message will split and rearrange the size based on its character length 
    */ 
    private void updateText(String message, TextView mTvMessageText) { 
     try { 
      if (message == null || message.length() == 0) { 
       return; 
      } 

      String word = getLongestWordLength(message); 

      if (word == null) { 
       return; 
      } 
      String wordUpper = word.toUpperCase();// Convert the word to uppercase to find the Maximum Space 
      // mTvMessageText - TextView need to Update the Value 
      float width = ((mTvMessageText.getMeasuredWidth()) - 120); // Get the width of the View with reduced padding 
      float textWidth = mTvMessageText.getPaint().measureText(wordUpper); // Get the word Holding Space through Paint 
      float textSizeInPixel = getResources().getDimension(R.dimen.message_size); // Get dimension text Size - My Size is 65sp 
      float lineSpacingExtra = getResources().getDimension(R.dimen.message_line_spacing); //High text size required Negative Line Spacing initially -15 

      /** 
      * Loop will reduce the font size of actual 3% in each looping 
      * The looping condition is the longest word in the sentence to hold in a single line of View 
      * Reduce the Inline space with accordingly 
      * Submit the reduced amount of size in the textView and check the holding pixels 
      * If the holding pixels are up above the total pixel size, the loop will continue 
      */ 
      while (textWidth > width) { 
       textSizeInPixel -= textSizeInPixel * (0.03); // Reduce the Fount Size with 3% each looping 
       lineSpacingExtra += Math.abs(lineSpacingExtra) * (0.06); // Reduce the minus space extra 
       this.mTvMessageText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeInPixel); 
       this.mTvMessageText.setLineSpacing(lineSpacingExtra, 1f); 
       textWidth = mTvMessageText.getPaint().measureText(wordUpper);// Assign value to measure the text Size 
      } 

      /** 
      * M & N devices has a property to rearrange the word with hyphenation 
      * In Order to avoid the same, Application will add this logic 
      */ 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       mTvMessageText.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE); 
      } 

      /** 
      * Text Set Using from Html 
      */ 

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
       this.mTvMessageText.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY)); 
      } else { 
       this.mTvMessageText.setText(Html.fromHtml(message)); 
      } 
     } catch (Resources.NotFoundException e) { 
      Log.e(TAG, e.getMessage()); 
     } 

    } 


    /** 
    * 
    * @param wordString - Raw String with Multiple word 
    *     This may be a header 
    *     May be a paragraph 
    *     May be contain Multiple Paragraphs 
    * @return - Identify the Longest word and return the length of it 
    */ 
    private String getLongestWordLength(String wordString) { 
     try { 
      if (wordString == null) { 
       return null; 
      } 
      if (wordString.length() == 0) { 
       return null; 
      } 
      String[] splitArray = wordString.split(" "); 

      String word = ""; 

      for (int i = 0; i < splitArray.length; i++) { 
       if (splitArray[i].length() > word.length()) { 
        word = splitArray[i]; 
       } 
      } 
      return word; 
     } catch (Exception e) { 
      Log.e(TAG, e.getMessage()); 
     } 
     return null; 
    } 
関連する問題