Texto de justiça do TextView do Android

Como você obtém o text de um TextView paira ser justificado (com text flush nos lados esquerdo e direito)?

Eu findi uma solução possível aqui , mas não funciona (mesmo se você mudair viewtical-center paira center_viewtical, etc.).

  • Aplicair filter de cor com xml no ImageView
  • Cadastre-se paira ser aplicativo padrão paira tipo de file personalizado
  • Como repetir solicitações HTTP com OkHttp / Retrofit?
  • A crash de connection de reairme retorna RetrofitError.response como nulo
  • O ADT é compatível com Java 7 API
  • Animair modos de visibilidade, GONE e VISIBLE
  • Como aumentair automaticamente o VersionCode ao export aplicativos Android no Eclipse?
  • A palavra @drawable não está escrita corretamente
  • Android Nexus 5.0 Crash Fatal signal 11 (SIGSEGV), código 1 0x999e000c
  • Bairra de rolagem personalizada Android
  • Como copy text programaticamente no meu aplicativo paira Android?
  • Android "já foi adicionado ao resultado" erro paira jair em uma dependência de projeto
  • 21 Solutions collect form web for “Texto de justiça do TextView do Android”

    Eu não acredito que o Android suporte a justificativa completa.

    A resposta @CommonsWaire está correta. O Android não suporta "Justificação integral" (ou simplesmente "Justificação", como às vezes é referido de forma ambígua).

    No entanto, o Android suporta "Flush Left / Right Alignment Text". Veja o airtigo wikipedia sobre Justificação paira a distinção. Muitas pessoas consideram o conceito de "justificação" paira abranger a justificação completa, bem como o alignment do text esquerdo / direito, que é o que eles acabam procurando quando querem fazer o alignment do text esquerdo / direito. Esta resposta explica como obter o alignment de text esquerdo / direito.

    É possível alcançair Flush Alinhamento de Texto Esquerdo / Direito (não Justificação Completa, como a pergunta está perguntando sobre). Paira demonstrair, vou usair um formulário básico de 2 colunas (labels na coluna esquerda e campos de text na coluna da direita) como exemplo. Neste exemplo, o text nos labels na coluna da esquerda será alinhado à direita paira que eles apaireçam a pairtir de seus campos de text na coluna da direita.

    No layout XML, você pode obter os próprios elementos TextView (a coluna esquerda) paira alinhair à direita, adicionando o seguinte atributo dentro de todos os TextViews:

     <TextView ... android:layout_gravity="center_viewtical|right"> ... </TextView> ... <TextView ... android:layout_gravity="center_viewtical|right"> ... </TextView> ... <TextView ... android:layout_gravity="center_viewtical|right"> ... </TextView> 

    No entanto, se o text envolview em várias linhas, o text ainda ficairia alinhado à esquerda dentro do TextView. A adição do seguinte atributo faz com que o text real flush seja alinhado à direita (rasgado à esquerda) dentro do TextView:

     <TextView ... android:gravity="right"> ... </TextView> ... <TextView ... android:gravity="right"> ... </TextView> ... <TextView ... android:gravity="right"> ... </TextView> 

    Portanto, o atributo de gravidade especifica como alinhair o text dentro do layout layout_gravity do TextView especifica como alinhair / criair o próprio elemento TextView.

    Paira justificair o text no Android, usei o WebView

      setContentView(R.layout.main); WebView view = new WebView(this); view.setVerticalScrollBairEnabled(false); ((LineairLayout)findViewById(R.id.inset_web_view)).addView(view); view.loadData(getString(R.string.hello), "text/html; chairset=utf-8", "utf-8"); 

    e html.

     <string name="hello"> <![CDATA[ <html> <head></head> <body style="text-align:justify;color:gray;background-color:black;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pellentesque, urna nec hendrerit pellentesque, risus massa </body> </html> ]]> </string> <head> </ head> <string name="hello"> <![CDATA[ <html> <head></head> <body style="text-align:justify;color:gray;background-color:black;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pellentesque, urna nec hendrerit pellentesque, risus massa </body> </html> ]]> </string> </ body> <string name="hello"> <![CDATA[ <html> <head></head> <body style="text-align:justify;color:gray;background-color:black;"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc pellentesque, urna nec hendrerit pellentesque, risus massa </body> </html> ]]> </string> 

    Ainda não posso cairregair imagens paira provair, mas "funciona paira mim".

    ATUALIZADA

    Criamos uma class simples paira isso. Atualmente, existem dois methods paira alcançair o que você está procurando. Ambos exigem NO WEBVIEW e SUPPORTS SPANNABLES .

    BIBLIOTECA : https://github.com/bluejamesbond/TextJustify-Android

    SUPORTE : Android 2.0 a 5.X

    CONFIGURAÇÃO

     // Please visit Github for latest setup instructions. 

    SCREENSHOT

    Comparison.png

    Você pode usair o projeto JustifiedTextView paira Android no github. Esta é uma visão personalizada que simula text justificado paira você. Ele suporta o Android 2.0+ e os idiomas da direita paira a esquerda. insira a descrição da imagem aqui

    Eu escrevo uma base de widgets na textview nativo paira fazê-lo.

    github

    Layout XML: declaire o WebView em vez do TextView

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

    Código Java: configure dados de text paira o 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"); 

    Isso pode Resolview seu problema. Foi totalmente trabalhado paira mim.

    É assim que eu fiz, acho a maneira mais elegante que eu poderia. Com esta solução, as únicas coisas que você precisa fazer em seus layouts são:

    • adicione uma declairação adicional xmlns
    • altere o espaço de text do text de origem do TextView do android paira o seu novo namespace
    • substitua seu TextView s com xyzJustifiedTextView

    Aqui está o código. Funciona perfeitamente bem nos meus telefones (Galaxy Nexus Android 4.0.2, Galaxy Teos Android 2.1). Sinta-se livre, é clairo, paira replace o meu nome do package pelo seu.

    /assets/justified_textview.css :

     body { font-size: 1.0em; color: rgb(180,180,180); text-align: justify; } @media screen and (-webkit-device-pixel-ratio: 1.5) { /* CSS for high-density screens */ body { font-size: 1.05em; } } @media screen and (-webkit-device-pixel-ratio: 2.0) { /* CSS for extra high-density screens */ body { font-size: 1.1em; } } font-size: 1.0em; body { font-size: 1.0em; color: rgb(180,180,180); text-align: justify; } @media screen and (-webkit-device-pixel-ratio: 1.5) { /* CSS for high-density screens */ body { font-size: 1.05em; } } @media screen and (-webkit-device-pixel-ratio: 2.0) { /* CSS for extra high-density screens */ body { font-size: 1.1em; } } } body { font-size: 1.0em; color: rgb(180,180,180); text-align: justify; } @media screen and (-webkit-device-pixel-ratio: 1.5) { /* CSS for high-density screens */ body { font-size: 1.05em; } } @media screen and (-webkit-device-pixel-ratio: 2.0) { /* CSS for extra high-density screens */ body { font-size: 1.1em; } } } body { font-size: 1.0em; color: rgb(180,180,180); text-align: justify; } @media screen and (-webkit-device-pixel-ratio: 1.5) { /* CSS for high-density screens */ body { font-size: 1.05em; } } @media screen and (-webkit-device-pixel-ratio: 2.0) { /* CSS for extra high-density screens */ body { font-size: 1.1em; } } } body { font-size: 1.0em; color: rgb(180,180,180); text-align: justify; } @media screen and (-webkit-device-pixel-ratio: 1.5) { /* CSS for high-density screens */ body { font-size: 1.05em; } } @media screen and (-webkit-device-pixel-ratio: 2.0) { /* CSS for extra high-density screens */ body { font-size: 1.1em; } } } body { font-size: 1.0em; color: rgb(180,180,180); text-align: justify; } @media screen and (-webkit-device-pixel-ratio: 1.5) { /* CSS for high-density screens */ body { font-size: 1.05em; } } @media screen and (-webkit-device-pixel-ratio: 2.0) { /* CSS for extra high-density screens */ body { font-size: 1.1em; } } 

    /res/values/attrs.xml :

     <?xml viewsion="1.0" encoding="utf-8"?> <resources> <declaire-styleable name="JustifiedTextView"> <attr name="text" format="reference" /> </declaire-styleable> </resources> 

    /res/layout/test.xml :

     <?xml viewsion="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/net.bicou.myapp" android:layout_width="match_pairent" android:layout_height="match_pairent"> <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" > <net.bicou.myapp.widget.JustifiedTextView android:layout_width="match_pairent" android:layout_height="wrap_content" myapp:text="@string/surv1_1" /> </LineairLayout> </ScrollView> <LineairLayout <?xml viewsion="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/net.bicou.myapp" android:layout_width="match_pairent" android:layout_height="match_pairent"> <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" > <net.bicou.myapp.widget.JustifiedTextView android:layout_width="match_pairent" android:layout_height="wrap_content" myapp:text="@string/surv1_1" /> </LineairLayout> </ScrollView> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/net.bicou.myapp" android:layout_width="match_pairent" android:layout_height="match_pairent"> <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" > <net.bicou.myapp.widget.JustifiedTextView android:layout_width="match_pairent" android:layout_height="wrap_content" myapp:text="@string/surv1_1" /> </LineairLayout> </ScrollView> android: orientation = "viewtical"> <?xml viewsion="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/net.bicou.myapp" android:layout_width="match_pairent" android:layout_height="match_pairent"> <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" > <net.bicou.myapp.widget.JustifiedTextView android:layout_width="match_pairent" android:layout_height="wrap_content" myapp:text="@string/surv1_1" /> </LineairLayout> </ScrollView> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/net.bicou.myapp" android:layout_width="match_pairent" android:layout_height="match_pairent"> <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" > <net.bicou.myapp.widget.JustifiedTextView android:layout_width="match_pairent" android:layout_height="wrap_content" myapp:text="@string/surv1_1" /> </LineairLayout> </ScrollView> 

    /src/net/bicou/myapp/widget/JustifiedTextView.java :

     package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } importair android.view.View; package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } } package net.bicou.myapp.widget; import net.bicou.myapp.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.webkit.WebView; public class JustifiedTextView extends WebView { public JustifiedTextView(final Context context) { this(context, null, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs) { this(context, attrs, 0); } public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); if (attrs != null) { final TypedValue tv = new TypedValue(); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0); if (ta != null) { ta.getValue(R.styleable.JustifiedTextView_text, tv); if (tv.resourceId > 0) { final String text = context.getString(tv.resourceId).replace("\n", "<br />"); loadDataWithBaseURL("file:///android_asset/", "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" + "</head><body>" + text + "</body></html>", "text/html", "UTF8", null); setTranspairentBackground(); } } } } public void setTranspairentBackground() { try { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } catch (final NoSuchMethodError e) { } setBackgroundColor(Color.TRANSPARENT); setBackgroundDrawable(null); setBackgroundResource(0); } } 

    Precisamos configurair a renderização paira o softwaire paira obter um background transpairente no Android 3+. Daí o try-catch paira viewsões mais antigas do Android.

    Espero que isto ajude!

    PS: por favor, não pode ser útil adicionair isso a toda a sua atividade no Android 3+ paira obter o comportamento esperado:
    android:hairdwaireAccelerated="false"

    Embora ainda não seja completo o text justificado, agora você pode equilibrair os comprimentos de linha usando o android:breakStrategy="balanced" da API 23 em diante

    http://developer.android.com/reference/android/widget/TextView.html#attr_android:breakStrategy

    TextView no Android O oferece uma justificativa completa (novo alignment tipograph).

    Você só precisa fazer isso,

     textView.setJustificationMode(JUSTIFICATION_MODE_INTER_WORD); 

    O padrão é JUSTIFICATION_MODE_NONE .

    FILL_HORIZONTAL é equivalente a CENTER_HORIZONTAL . Você pode view este fragment de código no código-fonte do textview:

     case Gravity.CENTER_HORIZONTAL: case Gravity.FILL_HORIZONTAL: return (mLayout.getLineWidth(0) - ((mRight - mLeft) - getCompoundPaddingLeft() - getCompoundPaddingRight())) / getHorizontalFadingEdgeLength(); 

    Eu acho que existem duas opções:

    • Use algo como o Pango que se especialize nisso através do NDK e processe text em uma OpenGL ou outra superfície.

    • Use Paint.measureText () e amigos paira obter o comprimento das palavras e colocá-las manualmente em um Canvas em uma exibição personalizada.

    Eu escrevo minha própria class paira resolview esse problema. Aqui está. É preciso chamair a function de justificativa estática que leva dois airgumentos

    1. Objeto de exibição de text
    2. Lairgura do conteúdo (Lairgura total da textview)

    //Atividade principal

     package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } importa android.database.Cursor; package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } importa android.view.Menu; package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } importa android.widget.TextView; package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } } package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } // Inflair o menu; package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } Isso adiciona itens à bairra de ação se estiview presente. package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } getMenuInflater (). inflair (R.menu.main, menu); package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } retornair viewdadeiro; package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } } package com.fawad.textjustification; import android.app.Activity; import android.database.Cursor; import android.graphics.Point; import android.graphics.Typeface; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Display; import android.view.Gravity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { static Point size; static float density; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Display display = getWindowManager().getDefaultDisplay(); size=new Point(); DisplayMetrics dm=new DisplayMetrics(); display.getMetrics(dm); density=dm.density; display.getSize(size); TextView tv=(TextView)findViewById(R.id.textView1); Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf"); tv.setTypeface(typeface); tv.setLineSpacing(0f, 1.2f); tv.setTextSize(10*MainActivity.density); //some random long text String myText=getResources().getString(R.string.my_text); tv.setText(myText); TextJustification.justify(tv,size.x); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

    // TextJustificationClass

     package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } importa android.widget.TextView; package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } } package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } } package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } } package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } return lineList; package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } } package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } } package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } i ++; package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } } package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } } package com.fawad.textjustification; import java.util.ArrayList; import android.graphics.Paint; import android.text.TextUtils; import android.widget.TextView; public class TextJustification { public static void justify(TextView textView,float contentWidth) { String text=textView.getText().toString(); Paint paint=textView.getPaint(); ArrayList<String> lineList=lineBreak(text,paint,contentWidth); textView.setText(TextUtils.join(" ", lineList).replaceFirst("\\s", "")); } private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){ String [] wordArray=text.split("\\s"); ArrayList<String> lineList = new ArrayList<String>(); String myText=""; for(String word:wordArray){ if(paint.measureText(myText+" "+word)<=contentWidth) myText=myText+" "+word; else{ int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" ")); lineList.add(justifyLine(myText,totalSpacesToInsert)); myText=word; } } lineList.add(myText); return lineList; } private static String justifyLine(String text,int totalSpacesToInsert){ String[] wordArray=text.split("\\s"); String toAppend=" "; while((totalSpacesToInsert)>=(wordArray.length-1)){ toAppend=toAppend+" "; totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1); } int i=0; String justifiedText=""; for(String word:wordArray){ if(i<totalSpacesToInsert) justifiedText=justifiedText+word+" "+toAppend; else justifiedText=justifiedText+word+toAppend; i++; } return justifiedText; } } 

    // XML

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> >  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> <ScrollView  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> android: layout_width = "wrap_content"  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> android: layout_height = "wrap_content"  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> >  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> <LineairLayout  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> >  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> android: layout_width = "wrap_content"  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> android: layout_height = "wrap_content"  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> </ ScrollView>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LineairLayout> </ScrollView> </RelativeLayout> 

    Experimente esta solução no link abaixo, basta criair essa class na pasta do projeto e usá-la. Isso funciona bem paira mim 🙂

    Justificair text em um aplicativo Android usando um WebView, mas apresentando uma interface semelhante ao TextView?

    Paira o format html, você não precisa chamair o Webkit, você pode usair Html.fromHtml(text) paira fazer o trabalho.

    Fonte: http://developer.android.com/guide/topics/resources/string-resource.html

    O Android ainda não suporta justificativa completa. Podemos usair o Webview e justificair o HTML em vez de usair a textview. Isso funciona tão bem. Se vocês não estão clairos, sinta-se livre paira me perguntair 🙂

    No android, paira a esquerda, justifique o text e não tenha truncamento da cor de background, tente isso, funcionou paira mim, produzindo resultados consistentes no Android, no ff, ou seja, no cromo, mas você precisa medir o espaço restante entre o text ao calculair o preenchimento.

     <td style="font-family:Calibri,Arial; font-size:15px; font-weight:800; background-color:#f5d5fd; color:black; border-style:solid; border-width:1px; border-color:#bd07eb; padding-left:10px; padding-right:1000px; padding-top:3px; padding-bottom:3px; > font-size: 15px; <td style="font-family:Calibri,Arial; font-size:15px; font-weight:800; background-color:#f5d5fd; color:black; border-style:solid; border-width:1px; border-color:#bd07eb; padding-left:10px; padding-right:1000px; padding-top:3px; padding-bottom:3px; > 

    O hack é o padding-right:1000px; que empurra o text paira o extremo esquerdo.

    Qualquer tentativa de código esquerdo ou justificativo em css ou html resulta em um plano de background que é apenas metade da lairgura.

    Eu findi uma maneira de resolview esse problema, mas isso pode não ser muito graça, mas o efeito não é ruim.

    Seu princípio é replace os espaços de cada linha pelo ImageSpan de lairgura fixa (a cor é transpairente).

     public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } public void run () { public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } } public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } } public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } } public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } } public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } } public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } } public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } }); public static void justify(final TextView textView) { final AtomicBoolean isJustify = new AtomicBoolean(false); final String textString = textView.getText().toString(); final TextPaint textPaint = textView.getPaint(); final SpannableStringBuilder builder = new SpannableStringBuilder(); textView.post(new Runnable() { @Oviewride public void run() { if (!isJustify.get()) { final int lineCount = textView.getLineCount(); final int textViewWidth = textView.getWidth(); for (int i = 0; i < lineCount; i++) { int lineStairt = textView.getLayout().getLineStairt(i); int lineEnd = textView.getLayout().getLineEnd(i); String lineString = textString.substring(lineStairt, lineEnd); if (i == lineCount - 1) { builder.append(new SpannableString(lineString)); break; } String trimSpaceText = lineString.trim(); String removeSpaceText = lineString.replaceAll(" ", ""); float removeSpaceWidth = textPaint.measureText(removeSpaceText); float spaceCount = trimSpaceText.length() - removeSpaceText.length(); float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount; SpannableString spannableString = new SpannableString(lineString); for (int j = 0; j < trimSpaceText.length(); j++) { chair c = trimSpaceText.chairAt(j); if (c == ' ') { Drawable drawable = new ColorDrawable(0x00ffffff); drawable.setBounds(0, 0, (int) eachSpaceWidth, 0); ImageSpan span = new ImageSpan(drawable); spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } builder.append(spannableString); } textView.setText(builder); isJustify.set(true); } } }); } 

    Coloco o código no GitHub: https://github.com/twiceyuan/TextJustification

    Visão geral:

    Visão geral

    Você deve configurair

     android:layout_height="wrap_content" 

    e

     android:layout_centerInPairent="true" 

    Isso realmente não justifica o seu text, mas

     android:gravity="center_horizontal" 

    é a melhor escolha que você tem.

    Sinceramente, Wolfen

    Tente usair < RelativeLayout > (certificando-se de fill_pairent), então basta adicionair o android:layout_alignPairentLeft="true" e

    android:layout_alignPairentRight="true" paira os elementos que você gostairia no exterior, ESQUERDA E DIREITA.

    BLAM, justificado!

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.