2017-08-30 5 views
0

Je me sers d'une bibliothèque pour redimensionner le texte à insérer dans les limites: https://github.com/grantland/android-autofittextview. Ce que j'essaie de faire est de faire défiler une ArrayList d'affichage de texte et de trouver la taille de texte la plus basse, puis de définir cette taille de texte à chaque vue de texte. Mon code Java:Android - la taille du texte mis en coupe de mots programatically

//Loop through every textview and get lowest size 
     float lowestTextSize = textViews.get(0).getTextSize(); 
     for (TextView textView : textViews){ 
      if(lowestTextSize > textView.getTextSize()){ 
       lowestTextSize = textView.getTextSize(); 
      } 
     } 

     lowestTextSize = lowestTextSize/getResources().getDisplayMetrics().scaledDensity; 

     //Loop through every textview and set them all to the lowest text size previously found 
     for (TextView textView : textViews){ 
      textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, lowestTextSize); 
     } 

Mon problème est que, après la mise en cette taille à chaque textview la taille des changements de TextView, mais pas la taille des se dire, si je reçois des mots coupés en deux (exception faisant la mot dont la taille que j'utilise, celle redimensionnée par la bibliothèque AutoFit): enter image description here

Une idée pour résoudre ce problème?

Mon code XML:

<LinearLayout 
    android:id="@+id/highestSellingProductLayout" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="8dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="8dp" 
    android:layout_weight="1" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:gravity="center" 
     android:orientation="horizontal"> 

     <me.grantland.widget.AutofitTextView 
      android:id="@+id/highestSellingProduct" 
      autofit:minTextSize="15sp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:maxLines="1" 
      android:paddingBottom="5dp" 
      android:text="NA" 
      android:textAlignment="center" 
      android:textColor="@color/colorSuccess" 
      android:textSize="22sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <me.grantland.widget.AutofitTextView 
      autofit:minTextSize="15sp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="bottom" 
      android:maxLines="1" 
      android:paddingBottom="5dp" 
      android:text="Highest Selling" 
      android:textAlignment="center" 
      android:textSize="15sp" /> 
    </LinearLayout> 

</LinearLayout> 
+2

Augmenter la hauteur de mise en page –

+0

est inutile. parce que mon but est de redimensionner le texte selon le texte de taille la plus basse ... comme vous pouvez le voir le texte n'est pas redimensionné sa coupure .... il reste la même taille – Alphonse

+0

getTextSize() renvoie les dimensions de pixel réelles du texte . Essayez TypedValue.COMPLEX_UNIT_PX – PrisonMike

Répondre

0

j'ai trouvé la question. Pour modifier la taille du texte dans Java en utilisant AutofitTextView, j'ai dû utiliser autofitHelper sur chaque textview. Le code suivant a fonctionné pour moi:

//Loop through every textview and get lowest size 
    float lowestTextSize = textViews.get(0).getTextSize(); 
    for (TextView textView : textViews){ 
     if(lowestTextSize > textView.getTextSize()){ 
      lowestTextSize = textView.getTextSize(); 
     } 
    } 

    lowestTextSize = lowestTextSize/getResources().getDisplayMetrics().scaledDensity; 

    //Loop through every textview and set them all to the lowest text size previously found 
    for (TextView textView : textViews){ 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, lowestTextSize); 
     AutofitHelper autofitHelper = AutofitHelper.create(textView); 
     autofitHelper.setTextSize(textView.getTextSize()); 
    }