La marquesina TextView no funciona [duplicar]


Esta pregunta ya tiene una respuesta aquí:

He intentado usar marquesina y no funciona aquí está mi código, por favor hágamelo saber donde estoy yendo mal

<TextView
   android:text="lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00"
   android:id="@+id/TextView02"
   android:layout_width="200dip"
   android:layout_height="wrap_content"
   android:marqueeRepeatLimit="marquee_forever"
   android:ellipsize="marquee"
   android:singleLine="true"
   android:focusable="true"
   android:inputType="text"
   android:maxLines="1">
</TextView>

Estoy usando android SDK 2.0.1

Author: Janusz, 2010-07-26

21 answers

Trabajando ahora :) Código adjunto a continuación

<TextView
    android:text="START | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | END"
    android:id="@+id/MarqueeText" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:singleLine="true"
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true" 
    android:paddingLeft="15dip" 
    android:paddingRight="15dip" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:freezesText="true">

Editar (en nombre de Adil Hussain):

textView.setSelected(true) necesita ser puesto en código detrás para que esto funcione.

 238
Author: amithgc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-14 07:40:54
android:singleLine="true"
android:ellipsize="marquee"

Son los únicos atributos requeridos y el desplazamiento incluso funciona con layout_weight definido con layout_width=0dp

Aquí hay un código de ejemplo:

<TextView 
            android:id="@+id/scroller"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF"
            android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
            android:layout_marginLeft="4dp"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

Pero lo más importante es implícitamente o explícitamente TextView debe ser seleccionado.

Puedes hacer esto con:

TextView txtView=(TextView) findViewById(R.id.scroller);
txtView.setSelected(true);
 82
Author: Shardul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-15 08:38:55

Estos atributos deben incluirse en la etiqueta textview para permitir el desplazamiento.

Todo lo demás es opcional.

android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:ellipsize="marquee"
 23
Author: sajamaum,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-14 18:23:20

Me enfrenté al mismo problema y esta discusión me ayudó a reemplazar esta línea

android:maxLines="1"

Con esta línea en xml

android:singleLine="true"

Y funciona la línea txtView.setSelected(true); también estaba en mi actividad.

 19
Author: Dulal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-17 12:26:37

Código de trabajo muy simple:

Para texto infinitamente desplazable

            <TextView
            android:id="@+id/textView_News_HeadLine"
            style="@style/black_extra_large_heading_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="8dp"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="-1"
            android:singleLine="true"
            android:text="HeadLine: Banglawash To be Continued" />

& debe escribir desde su actividad

textView.setSelected(true);
 12
Author: Shihab Uddin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-13 04:27:23

Código de trabajo:

<TextView
    android:id="@+id/scroller"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
    android:textAppearance="?android:attr/textAppearanceLarge" />
 9
Author: Kirit Vaghela,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-06 07:45:56

Estoy trabajando con minSdk=14 y tenía curiosidad por saber qué conjunto de estas variaciones funcionaría. Terminé con:

android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"

Además de otras cosas de formato. No necesitaba desplazarme horizontalmente, enfocable o enfocable en el modo táctil.

Este conjunto requirió una llamada a

setSelected(true)

Lo que encuentro interesante es que singleLine supuestamente ha sido obsoleto, con una recomendación de reemplazarlo con maxLines = 1. Excepto-cuando hago eso, ese cambio por sí solo detiene el texto de desplazar. Uno esperaría que cuando singleLine finalmente muerda el polvo, que todo su comportamiento actual sea activado por maxLines...

 7
Author: ssdscott,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-04 16:24:54

Había pasado por esta situación en la que textview marquee no estaba funcionando. Sin embargo, siga esto y estoy seguro de que funcionará. :)

<TextView
         android:id="@+id/tv_marquee"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ellipsize="marquee"
         android:focusable="true"
         android:focusableInTouchMode="true"
         android:freezesText="true"
         android:maxLines="1"
         android:scrollHorizontally="true"
         android:text="This is a sample code of marquee and it works"/>

Y añade programáticamente estas 2 líneas...

tvMarquee.setHorizontallyScrolling(true);
tvMarquee.setSelected(true);

TvMarquee.setSelected (true) es necesario en caso de que alguna de las vistas ya esté enfocada y setSelected haga que funcione. No hay necesidad de usar.

android:singleLine="true"

Está obsoleto y los códigos anteriores funcionan.

 7
Author: Vijay E,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-04 08:05:46

Me he encontrado con el mismo problema. La respuesta de Amith GC (la primera respuesta comprobada como aceptada) es correcta, pero a veces textview.setSelected (true); no funciona cuando la vista de texto no puede obtener el enfoque todo el tiempo. Por lo tanto, para garantizar que TextView Marquee funcione, tuve que usar un TextView personalizado.

public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }


    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }


    @Override
    public boolean isFocused() {
        return true;
    }
}

Y luego, puede usar la vista de texto personalizada como la vista de texto de desplazamiento en su diseño .archivo xml como este:

<com.example.myapplication.CustomTextView
            android:id="@+id/tvScrollingMessage"
            android:text="@string/scrolling_message_main_wish_list"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/black"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="15dp"
            android:freezesText="true"/>

NOTA: en el fragmento de código anterior com.ejemplo.myapplication es un ejemplo de nombre de paquete y debe ser reemplazado por su propio nombre de paquete.

Espero que esto te ayude. ¡Salud!

 6
Author: David Roman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-16 10:51:47

Simplemente agregue los que se dijo anteriormente:

    android:singleLine="true" 
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"

Y!! debe usar TextView.setSelected (true) dentro de su código java.

La razón por la que marquee no funciona con algunos de los chicos en este artículo , si tiene un formulario de entrada con un EditText ( que es una entrada), el EditText será el que tenga enfoque y selección por defecto en el formulario. Ahora, si lo fuerzas a través de TextView.setSelected (true), TextView eventualmente hará marquesina sin importar qué.

Así que todo el la idea es hacer que el widget TextView se enfoque y se seleccione para que funcione la marquesina.

 5
Author: superlinux,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-13 13:41:05
 <TextView
  android:ellipsize="marquee"
  android:singleLine="true"
  .../>

Debe llamar al código

textView.setSelected(true);
 5
Author: susemi99,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-19 00:45:31

Debe agregar estos atributos que es obligatorio para marquee

 android:ellipsize="marquee"     
 android:focusable="true"    
 android:focusableInTouchMode="true"     
 android:singleLine="true"     
 android:marqueeRepeatLimit="marquee_forever"     
 android:scrollHorizontally="true"
 4
Author: Chandan kushwaha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-03 16:36:46
package com.app.relativejavawindow;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final RelativeLayout relativeLayout = new RelativeLayout(this);
        final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
        textView = new TextView(this);
        textView.setId(1);

        RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        relativeLayoutbotombar.setLayoutParams(relativlaybottombar);


        textView.setText("Simple application that shows how to use marquee, with a long ");
        textView.setEllipsize(TruncateAt.MARQUEE);
        textView.setSelected(true);
        textView.setSingleLine(true);


        relativeLayout.addView(relativeLayoutbotombar);
        relativeLayoutbotombar.addView(textView);
        //relativeLayoutbotombar.setBackgroundColor(Color.BLACK);
        setContentView(relativeLayout, relativlayparamter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Este código funciona correctamente, pero si el tamaño de la pantalla no es llenar este texto no se moverá tratar de palcing espacio en blanco final del texto

 3
Author: harsha.kuruwita,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-05-28 04:17:12

Utilice la siguiente línea en su código:

TextView.setSelected(true);
 3
Author: Nima K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-04 14:01:49

marquee_forever también trabajo en el caso de ancho fijo para TextView. (por ejemplo, android:layout_width="120dp")

Los atributos necesarios son:

  1. android: focusable = "true"
  2. android: focusableInTouchMode = "true"
  3. android:singleLine="true" // si falta texto aparecerá en varias líneas.

Código de trabajo:

<TextView
                android:id="@+id/mediaTitleTV"
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:singleLine="true"
                android:text="Try Marquee, it works with fixed size textview smoothly!" />
 2
Author: Monika,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-01 20:18:30

Simplemente ponga estos parámetros en su TextView. Funciona :)

    android:singleLine="true" 
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:scrollHorizontally="true"
    android:focusable="true"
    android:focusableInTouchMode="true" 

`

 2
Author: XYZ_deve,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-08 11:43:36

He creado una clase personalizada AlwaysMarqueTextView

public class AlwaysMarqueeTextView extends TextView
{
    protected boolean a;

    public AlwaysMarqueeTextView(Context context)
    {
        super(context);
        a = false;
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attributeset)
    {
        super(context, attributeset);
        a = false;
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attributeset, int i)
    {
        super(context, attributeset, i);
        a = false;
    }

    public boolean isFocused()
    {
        return a || super.isFocused();
    }

    public void setAlwaysMarquee(boolean flag)
    {
        setSelected(flag);
        setSingleLine(flag);
        if(flag)
        setEllipsize(TruncateAt.MARQUEE);
        a = flag;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) 
    {
        if(focused)

            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused)
    {
        if(focused)
            super.onWindowFocusChanged(focused);
    }
}

Y puedes comenzar a marcar cuando lo desees.. como

//textView.setSelected(true); No need of Selection..
textview.setAlwaysMarquee(true); 
 2
Author: Xar E Ahmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-02 09:18:57

Para tener su propia velocidad de desplazamiento y flexibilidad para personalizar las propiedades de marquesina, use lo siguiente:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:lines="1"
    android:id="@+id/myTextView"
    android:padding="4dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Simple application that shows how to use marquee, with a long text" />

Dentro de su actividad:

private void setTranslation() {
        TranslateAnimation tanim = new TranslateAnimation(
                TranslateAnimation.ABSOLUTE, 1.0f * screenWidth,
                TranslateAnimation.ABSOLUTE, -1.0f * screenWidth,
                TranslateAnimation.ABSOLUTE, 0.0f,
                TranslateAnimation.ABSOLUTE, 0.0f);
        tanim.setDuration(1000);
        tanim.setInterpolator(new LinearInterpolator());
        tanim.setRepeatCount(Animation.INFINITE);
        tanim.setRepeatMode(Animation.ABSOLUTE);

        textView.startAnimation(tanim);
    } 
 2
Author: Jaydev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-21 08:20:28

Android:focusable="true" y android:focusableInTouchMode="true" son esenciales....

Porque probé todos los demás sin estas líneas y el resultado fue sin marquesina. Cuando añado esto empezó a sobresalir..

 0
Author: poly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-28 07:48:45

introduzca la descripción de la imagen aquí

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.TextUtils.TruncateAt;
 import android.widget.TextView;

 public class MainActivity extends Activity {
 private TextView textview;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textview = (TextView) this.findViewById(R.id.textview);
    textview.setSelected(true);

    textview.setEllipsize(TruncateAt.MARQUEE);
    textview.setSingleLine(true);
 }

 }

Para más referencias haga clic aquí http://androiddhina.blogspot.in/2015/10/marquee-effect-in-android-textview.html

 0
Author: Dhina k,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-08 06:06:54

La mayoría de las respuestas son idénticas,
pero noten también que en algunos casos la marquesina no trabajará sin especificar de la anchura en las inmersiones para el contenedor.
Por ejemplo, si usa peso en el contenedor padre

android:layout_width="0dp"
android:layout_weight="0.5"

Marquee puede no funcionar.

 0
Author: Leo Droidcoder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-17 10:14:02