2010-08-24 4 views

Répondre

0

Il semblerait qu'Android ne prenne pas en charge la justification complète. :(

Le mieux que vous pouvez obtenir est à gauche ou à droite Justification.

Android TextView Justify Text

0

Vous pouvez également la largeur de la TextView pour envelopper son contenu (« wrap_content ») et justifier la TextView dans sa . Mise en page parent

0

utilisez le code suivant:

 public class TextJustify { 

     final static String SYSTEM_NEWLINE = "\n"; 
     final static float COMPLEXITY = 5.12f; // Reducing this will increase 
            // efficiency but will decrease 
            // effectiveness 
     final static Paint p = new Paint(); 

     /* @author Mathew Kurian */ 

    public static void run(final TextView tv, float origWidth, 
      int paddingLeft, int paddingRight, int marginLeft, int marginRight) { 


    origWidth-= paddingRight+marginRight+paddingLeft+marginLeft; 
    String s = tv.getText().toString(); 
    p.setTypeface(tv.getTypeface()); 
    String[] splits = s.split(SYSTEM_NEWLINE); 
    float width = origWidth - 5; 
    for (int x = 0; x < splits.length; x++) 
     if (p.measureText(splits[x]) > width) { 
      splits[x] = wrap(splits[x], width, p); 
      String[] microSplits = splits[x].split(SYSTEM_NEWLINE); 
      for (int y = 0; y < microSplits.length - 1; y++) 
       microSplits[y] = justify(removeLast(microSplits[y], " "), 
         width, p); 
      StringBuilder smb_internal = new StringBuilder(); 
      for (int z = 0; z < microSplits.length; z++) 
       smb_internal.append(microSplits[z] 
         + ((z + 1 < microSplits.length) ? SYSTEM_NEWLINE 
           : "")); 
      splits[x] = smb_internal.toString(); 
     } 
    final StringBuilder smb = new StringBuilder(); 
    for (String cleaned : splits) 
     smb.append(cleaned + SYSTEM_NEWLINE); 
    tv.setGravity(Gravity.RIGHT); 
    tv.setText(smb); 
    } 

    private static String wrap(String s, float width, Paint p) { 

    String[] str = s.split("\\s"); // regex 
    StringBuilder smb = new StringBuilder(); // save memory 
    smb.append(SYSTEM_NEWLINE); 
    for (int x = 0; x < str.length; x++) { 
     float length = p.measureText(str[x]); 
     String[] pieces = smb.toString().split(SYSTEM_NEWLINE); 
     try { 
      if (p.measureText(pieces[pieces.length - 1]) + length > width) 
       smb.append(SYSTEM_NEWLINE); 
     } catch (Exception e) { 
     } 
     smb.append(str[x] + " "); 
    } 
    return smb.toString().replaceFirst(SYSTEM_NEWLINE, ""); 
    } 

private static String removeLast(String s, String g) { 
     if (s.contains(g)) { 
      int index = s.lastIndexOf(g); 
      int indexEnd = index + g.length(); 
      if (index == 0) 
       return s.substring(1); 
      else if (index == s.length() - 1) 
       return s.substring(0, index); 
      else 
       return s.substring(0, index) + s.substring(indexEnd); 
    } 
    return s; 
} 

private static String justifyOperation(String s, float width, Paint p) { 
    float holder = (float) (COMPLEXITY * Math.random()); 
    while (s.contains(Float.toString(holder))) 
     holder = (float) (COMPLEXITY * Math.random()); 
     String holder_string = Float.toString(holder); 
    float lessThan = width; 
    int timeOut = 100; 
    int current = 0; 
    while (p.measureText(s) < lessThan && current < timeOut) { 
     s = s.replaceFirst(" ([^" + holder_string + "])", " " 
       + holder_string + "$1"); 
      lessThan = p.measureText(holder_string) + lessThan 
       - p.measureText(" "); 
     current++; 
    } 
    String cleaned = s.replaceAll(holder_string, " "); 
    return cleaned; 
} 

private static String justify(String s, float width, Paint p) { 
    while (p.measureText(s) < width) { 
     s = justifyOperation(s, width, p); 
    } 
return s; 
} 
    } 

et pour appeler ce que je crée la formule, vous devez utiliser le code suivant :

public static final int FinallwidthDp = 320 ; 
public static final int widthJustify = 223 ; 

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
int widthPixels = metrics.widthPixels; 

float scaleFactor = metrics.density; 
float widthDp = widthPixels/scaleFactor; 

TextView tv = (TextView) findViewById(R.id.textView1); 
ViewGroup.MarginLayoutParams lp1 = (ViewGroup.MarginLayoutParams) tv.getLayoutParams(); 

tv.setText(text); 
TextJustify.run(tv,widthDp/FinallwidthDp * widthJustify , tv.getPaddingLeft(),tv.getPaddingRight() , lp1.leftMargin, lp1.rightMargin); 

cet algorithme testé sur différents appareil et a bien fonctionné dans l'activité normale (pas de dialogue) et wrap-content largeur pour TextView, et a travaillé avec tous les rembourrage et margin.if pas bon pour vous, vous pouvez changer widthJustify jusqu'à ce regard bon pour vous, je l'espère ce utile

0

Mise en page XML: au lieu de déclarer WebView TextView

<WebView 
android:id="@+id/textContent" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" /> 

code Java: des données de texte mis à WebView

WebView view = (WebView) findViewById(R.id.textContent); 
String text; 
text = "<html><body><p align=\"justify\">"; 
text+= "This is the text will be justified when displayed!!!"; 
text+= "</p></body></html>"; 
view.loadData(text, "text/html", "utf-8"); 

Cela peut résoudre votre problème.

Questions connexes