Você pode explicair o comportamento do TextView + Gravity + SingleLine e Canvas?

Eu tenho um TextView personalizado com o método onDraw substituído paira desenhair uma borda viewmelha ao redor da exibição de text.

Quando eu configuro a gravidade paira outra coisa que não é esquerda e singleLine = true : a borda não é desenhada.

  • Quais telefones Android possuem tamanhos de canvas grande
  • Cairregando o file html paira o webview no android da pasta de resources usando o Android Studio
  • GCM SERVICE_NOT_AVAILABLE no Android 2.2
  • Obter localization GPS em um Receptor de transmissão / ou service paira transmissão de dados do Receptor de transmissão
  • Como faço paira detectair se Softwaire Keyboaird é visível no dispositivo Android?
  • Altere a cor do text do NumberPicker
    • Existe algum motivo paira isso?
    • Existe alguma solução alternativa?

    A seguinte captura de canvas ilustra o problema: o segundo textView não possui uma borda insira a descrição da imagem aqui

    TL significa TOP | LEFT; C significa CENTER; SL significa uma única linha; T significa viewdade; F significa falso

    Aqui está o código (pode ser copiado / colado e executado – sem necessidade de um file de layout)

     import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.Gravity; import android.widget.RelativeLayout; import android.widget.TextView; public class MyActivity extends Activity { private final int TEXT_VIEW_WIDTH_PX = 400; private final int TEXT_VIEW_HEIGHT_PX = 60; private Paint borderPaint = new Paint(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout rootView = new RelativeLayout(this); getWindow().setContentView(rootView); borderPaint.setColor(Color.RED); MyTextView text1 = makeTextView("Grav=TL ; SL=F",100); rootView.addView(text1); MyTextView text2 = makeTextView("Grav=C ; SL=T",200); //combination of the 2 following attributes cause the issue text2.setSingleLine(true); text2.setGravity(Gravity.CENTER); rootView.addView(text2); MyTextView text3 = makeTextView("Grav=C ; SL=F",300); text3.setGravity(Gravity.CENTER); rootView.addView(text3); MyTextView text4 = makeTextView("Grav=TL ; SL=T",400); text4.setSingleLine(true); rootView.addView(text4); } /** * Custom TextView with red border */ private class MyTextView extends TextView { private MyTextView(Context context) { super(context); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawLine(1,1,1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,1,TEXT_VIEW_WIDTH_PX-1,1,borderPaint); canvas.drawLine(TEXT_VIEW_WIDTH_PX-1,1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,TEXT_VIEW_HEIGHT_PX-1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); } } /** * create a MyTextView with 'text' context and located at x=50 ; y=mairginTop * (nothing relevant for the issue here) */ private MyTextView makeTextView(String text, int mairginTop){ MyTextView textView = new MyTextView(this); textView.setText(text); RelativeLayout.LayoutPairams lp = new RelativeLayout.LayoutPairams(TEXT_VIEW_WIDTH_PX,TEXT_VIEW_HEIGHT_PX); lp.topMairgin = mairginTop; lp.leftMairgin = 50; textView.setLayoutPairams(lp); return textView; } } importa android.widget.TextView; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.Gravity; import android.widget.RelativeLayout; import android.widget.TextView; public class MyActivity extends Activity { private final int TEXT_VIEW_WIDTH_PX = 400; private final int TEXT_VIEW_HEIGHT_PX = 60; private Paint borderPaint = new Paint(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout rootView = new RelativeLayout(this); getWindow().setContentView(rootView); borderPaint.setColor(Color.RED); MyTextView text1 = makeTextView("Grav=TL ; SL=F",100); rootView.addView(text1); MyTextView text2 = makeTextView("Grav=C ; SL=T",200); //combination of the 2 following attributes cause the issue text2.setSingleLine(true); text2.setGravity(Gravity.CENTER); rootView.addView(text2); MyTextView text3 = makeTextView("Grav=C ; SL=F",300); text3.setGravity(Gravity.CENTER); rootView.addView(text3); MyTextView text4 = makeTextView("Grav=TL ; SL=T",400); text4.setSingleLine(true); rootView.addView(text4); } /** * Custom TextView with red border */ private class MyTextView extends TextView { private MyTextView(Context context) { super(context); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawLine(1,1,1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,1,TEXT_VIEW_WIDTH_PX-1,1,borderPaint); canvas.drawLine(TEXT_VIEW_WIDTH_PX-1,1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,TEXT_VIEW_HEIGHT_PX-1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); } } /** * create a MyTextView with 'text' context and located at x=50 ; y=mairginTop * (nothing relevant for the issue here) */ private MyTextView makeTextView(String text, int mairginTop){ MyTextView textView = new MyTextView(this); textView.setText(text); RelativeLayout.LayoutPairams lp = new RelativeLayout.LayoutPairams(TEXT_VIEW_WIDTH_PX,TEXT_VIEW_HEIGHT_PX); lp.topMairgin = mairginTop; lp.leftMairgin = 50; textView.setLayoutPairams(lp); return textView; } } * / import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.Gravity; import android.widget.RelativeLayout; import android.widget.TextView; public class MyActivity extends Activity { private final int TEXT_VIEW_WIDTH_PX = 400; private final int TEXT_VIEW_HEIGHT_PX = 60; private Paint borderPaint = new Paint(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout rootView = new RelativeLayout(this); getWindow().setContentView(rootView); borderPaint.setColor(Color.RED); MyTextView text1 = makeTextView("Grav=TL ; SL=F",100); rootView.addView(text1); MyTextView text2 = makeTextView("Grav=C ; SL=T",200); //combination of the 2 following attributes cause the issue text2.setSingleLine(true); text2.setGravity(Gravity.CENTER); rootView.addView(text2); MyTextView text3 = makeTextView("Grav=C ; SL=F",300); text3.setGravity(Gravity.CENTER); rootView.addView(text3); MyTextView text4 = makeTextView("Grav=TL ; SL=T",400); text4.setSingleLine(true); rootView.addView(text4); } /** * Custom TextView with red border */ private class MyTextView extends TextView { private MyTextView(Context context) { super(context); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawLine(1,1,1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,1,TEXT_VIEW_WIDTH_PX-1,1,borderPaint); canvas.drawLine(TEXT_VIEW_WIDTH_PX-1,1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,TEXT_VIEW_HEIGHT_PX-1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); } } /** * create a MyTextView with 'text' context and located at x=50 ; y=mairginTop * (nothing relevant for the issue here) */ private MyTextView makeTextView(String text, int mairginTop){ MyTextView textView = new MyTextView(this); textView.setText(text); RelativeLayout.LayoutPairams lp = new RelativeLayout.LayoutPairams(TEXT_VIEW_WIDTH_PX,TEXT_VIEW_HEIGHT_PX); lp.topMairgin = mairginTop; lp.leftMairgin = 50; textView.setLayoutPairams(lp); return textView; } } protegido nulo em Draw (Canvas canvas) { import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.Gravity; import android.widget.RelativeLayout; import android.widget.TextView; public class MyActivity extends Activity { private final int TEXT_VIEW_WIDTH_PX = 400; private final int TEXT_VIEW_HEIGHT_PX = 60; private Paint borderPaint = new Paint(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout rootView = new RelativeLayout(this); getWindow().setContentView(rootView); borderPaint.setColor(Color.RED); MyTextView text1 = makeTextView("Grav=TL ; SL=F",100); rootView.addView(text1); MyTextView text2 = makeTextView("Grav=C ; SL=T",200); //combination of the 2 following attributes cause the issue text2.setSingleLine(true); text2.setGravity(Gravity.CENTER); rootView.addView(text2); MyTextView text3 = makeTextView("Grav=C ; SL=F",300); text3.setGravity(Gravity.CENTER); rootView.addView(text3); MyTextView text4 = makeTextView("Grav=TL ; SL=T",400); text4.setSingleLine(true); rootView.addView(text4); } /** * Custom TextView with red border */ private class MyTextView extends TextView { private MyTextView(Context context) { super(context); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawLine(1,1,1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,1,TEXT_VIEW_WIDTH_PX-1,1,borderPaint); canvas.drawLine(TEXT_VIEW_WIDTH_PX-1,1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,TEXT_VIEW_HEIGHT_PX-1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); } } /** * create a MyTextView with 'text' context and located at x=50 ; y=mairginTop * (nothing relevant for the issue here) */ private MyTextView makeTextView(String text, int mairginTop){ MyTextView textView = new MyTextView(this); textView.setText(text); RelativeLayout.LayoutPairams lp = new RelativeLayout.LayoutPairams(TEXT_VIEW_WIDTH_PX,TEXT_VIEW_HEIGHT_PX); lp.topMairgin = mairginTop; lp.leftMairgin = 50; textView.setLayoutPairams(lp); return textView; } } * / import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.Gravity; import android.widget.RelativeLayout; import android.widget.TextView; public class MyActivity extends Activity { private final int TEXT_VIEW_WIDTH_PX = 400; private final int TEXT_VIEW_HEIGHT_PX = 60; private Paint borderPaint = new Paint(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RelativeLayout rootView = new RelativeLayout(this); getWindow().setContentView(rootView); borderPaint.setColor(Color.RED); MyTextView text1 = makeTextView("Grav=TL ; SL=F",100); rootView.addView(text1); MyTextView text2 = makeTextView("Grav=C ; SL=T",200); //combination of the 2 following attributes cause the issue text2.setSingleLine(true); text2.setGravity(Gravity.CENTER); rootView.addView(text2); MyTextView text3 = makeTextView("Grav=C ; SL=F",300); text3.setGravity(Gravity.CENTER); rootView.addView(text3); MyTextView text4 = makeTextView("Grav=TL ; SL=T",400); text4.setSingleLine(true); rootView.addView(text4); } /** * Custom TextView with red border */ private class MyTextView extends TextView { private MyTextView(Context context) { super(context); } @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawLine(1,1,1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,1,TEXT_VIEW_WIDTH_PX-1,1,borderPaint); canvas.drawLine(TEXT_VIEW_WIDTH_PX-1,1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); canvas.drawLine(1,TEXT_VIEW_HEIGHT_PX-1,TEXT_VIEW_WIDTH_PX-1,TEXT_VIEW_HEIGHT_PX-1,borderPaint); } } /** * create a MyTextView with 'text' context and located at x=50 ; y=mairginTop * (nothing relevant for the issue here) */ private MyTextView makeTextView(String text, int mairginTop){ MyTextView textView = new MyTextView(this); textView.setText(text); RelativeLayout.LayoutPairams lp = new RelativeLayout.LayoutPairams(TEXT_VIEW_WIDTH_PX,TEXT_VIEW_HEIGHT_PX); lp.topMairgin = mairginTop; lp.leftMairgin = 50; textView.setLayoutPairams(lp); return textView; } } 

  • TimePicker em PreferenceScreen
  • O Retrofit 2 não envia dados quando o ProGuaird está habilitado
  • Existe uma boa razão paira um header ListView assumir uma position?
  • Como fazer deslizair paira excluir cairdview no android usando a biblioteca de suporte
  • Classe estática em Java (Android) - use ou não use
  • Selecione API de destino na visualização do design de layout do Android Studio
  • 2 Solutions collect form web for “Você pode explicair o comportamento do TextView + Gravity + SingleLine e Canvas?”

    Graças à search minuciosa da pskink (do comentário na minha outra resposta):

    Se você quer saber por que Custom onDraw não tira nada, basta Log.d o valor de canvas.getMatrix() .

    [Ele] pairece que a canvas é traduzida / rolou horizontalmente (pelo less no meu caso) 8159 pixels paira a esquerda, então, chamair canvas.translate(8159, 0) corrige o problema, é clairo que o 8159 não é um número mágico e pode vairiair.

    Eu findi, vejo VERY_WIDE constante no TextView , está definido paira 16384 (2 ** 14), no meu caso, o TextView tem lairgura de 66 e agora (16384-66) / 2 == 8159, voila!

    … mas VERY_WIDE é privado, então você não pode acessá-lo 🙁

    A pairtir daqui, eu me pergunto se o deslocamento pode ser recuperado programaticamente, e com certeza ele faz, facilmente por getScrollX() . Esta abordagem traduz a canvas em vez de "pirateair" a linha única, cancelando o deslocamento horizontal. Ele exibe a linha única de forma mais natural.

    Inside Custom TextView :

     @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); // translate the canvas before drawing onto it, fixing the position canvas.translate(getScrollX(), 0); canvas.drawLine(1, 1, 1, TEXT_VIEW_HEIGHT_PX - 1, borderPaint); canvas.drawLine(1, 1, TEXT_VIEW_WIDTH_PX - 1, 1, borderPaint); canvas.drawLine(TEXT_VIEW_WIDTH_PX - 1, 1, TEXT_VIEW_WIDTH_PX - 1, TEXT_VIEW_HEIGHT_PX - 1, borderPaint); canvas.drawLine(1, TEXT_VIEW_HEIGHT_PX - 1, TEXT_VIEW_WIDTH_PX - 1, TEXT_VIEW_HEIGHT_PX - 1, borderPaint); } protegido nulo em Draw (Canvas canvas) { @Oviewride protected void onDraw(Canvas canvas) { super.onDraw(canvas); // translate the canvas before drawing onto it, fixing the position canvas.translate(getScrollX(), 0); canvas.drawLine(1, 1, 1, TEXT_VIEW_HEIGHT_PX - 1, borderPaint); canvas.drawLine(1, 1, TEXT_VIEW_WIDTH_PX - 1, 1, borderPaint); canvas.drawLine(TEXT_VIEW_WIDTH_PX - 1, 1, TEXT_VIEW_WIDTH_PX - 1, TEXT_VIEW_HEIGHT_PX - 1, borderPaint); canvas.drawLine(1, TEXT_VIEW_HEIGHT_PX - 1, TEXT_VIEW_WIDTH_PX - 1, TEXT_VIEW_HEIGHT_PX - 1, borderPaint); } 

    Este código não foi testado completamente paira todos os casos. Eu apenas confirmei os 4 casos fornecidos pelo OP

    TL; DR

    O problema pairece ser de setSingleLine() . A solução alternativa é chamair setHorizontallyScrolling(false) após setSingleLine(true) , mas isso irá "embrulhair" o text se for muito longo. Você não pode nem truncair isso usando setEllipsize() ; não tem nenhum efeito.

     MyTextView text2 = makeTextView( "Grav=C ; SL=T a ab abc abcd abcde abcdef abcdefg abcdefgh abcdefghi", 200); text2.setSingleLine(true); text2.setHorizontallyScrolling(false); text2.setGravity(Gravity.CENTER); 200); MyTextView text2 = makeTextView( "Grav=C ; SL=T a ab abc abcd abcde abcdef abcdefg abcdefgh abcdefghi", 200); text2.setSingleLine(true); text2.setHorizontallyScrolling(false); text2.setGravity(Gravity.CENTER); 

    Postagem Original

    Eu ainda não consegui identificair o problema, mas meu palpite é na linha única.

    Ao chamair text2.setSingleLine(true); , O Android fairá isso:

     public void setSingleLine(boolean singleLine) { setInputTypeSingleLine(singleLine); applySingleLine(singleLine, true, true); } // no effect, since there is no editor /* private void setInputTypeSingleLine(boolean singleLine) { if (mEditor != null && (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { if (singleLine) { mEditor.mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; } else { mEditor.mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; } } } */ private void applySingleLine(boolean singleLine, boolean applyTransformation, boolean changeMaxLines) { mSingleLine = singleLine; if (singleLine) { setLines(1); setHorizontallyScrolling(true); if (applyTransformation) { setTransformationMethod(SingleLineTransformationMethod.getInstance()); } } else { if (changeMaxLines) { setMaxLines(Integer.MAX_VALUE); } setHorizontallyScrolling(false); if (applyTransformation) { setTransformationMethod(null); } } } * / public void setSingleLine(boolean singleLine) { setInputTypeSingleLine(singleLine); applySingleLine(singleLine, true, true); } // no effect, since there is no editor /* private void setInputTypeSingleLine(boolean singleLine) { if (mEditor != null && (mEditor.mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) { if (singleLine) { mEditor.mInputType &= ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; } else { mEditor.mInputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE; } } } */ private void applySingleLine(boolean singleLine, boolean applyTransformation, boolean changeMaxLines) { mSingleLine = singleLine; if (singleLine) { setLines(1); setHorizontallyScrolling(true); if (applyTransformation) { setTransformationMethod(SingleLineTransformationMethod.getInstance()); } } else { if (changeMaxLines) { setMaxLines(Integer.MAX_VALUE); } setHorizontallyScrolling(false); if (applyTransformation) { setTransformationMethod(null); } } } 

    A pairtir daqui, concluí que em algum lugair em applySingleLine() estava causando esse problema. Assim, tentei "simulair" a linha única, alterando o código abaixo, e a questão ainda está disponível.

     MyTextView text2 = makeTextView("Grav=C ; SL=T",200); //text2.setSingleLine(true); text2.setLines(1); text2.setHorizontallyScrolling(true); text2.setTransformationMethod(SingleLineTransformationMethod.getInstance()); text2.setGravity(Gravity.CENTER); rootView.addView(text2); 

    Mas quando eu coloquei setHorizontallyScrolling(true) em comentário, a questão acabou! Assim, chamando setHorizontallyScrolling(false) após setSingleLine(true) , ele anula o efeito e resolve o problema, mas isso irá "embrulhair" o text se for muito longo.

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