2010-07-26 72 views
102

我曾尝试使用字幕和它不工作 这里是我的代码,请让我知道我去错了TextView的跑马灯不工作

<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> 

我使用的Android SDK 2.0.1

+1

它在Android SDK中工作1.5 – 2010-07-26 09:13:19

+0

@ Paresh你在2.0.1上测试过它吗? – amithgc 2010-07-26 10:02:13

+1

可能是它的后期,但它可以帮助别人..你必须以编程方式执行TextView.setSelected(true); – Nepster 2014-05-21 10:08:56

回答

210

工作现在:) 码连接下面

<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"> 

编辑(代表Adil Hussain):

textView.setSelected(true)需要在后面的代码中设置才能工作。

+5

了解您为改变工作而改变了什么? – 2010-07-26 12:12:25

+3

我已经尝试了许多变体的代码让选框工作,你的是唯一一个我已经成功。据我所知,这是所需要的marqueeing'机器人的最低限度:ID = “@ + ID /滚动条” \t \t \t机器人:layout_width = “FILL_PARENT” \t \t \t机器人:layout_height = “WRAP_CONTENT” \t \t \t机器人:线= “1” \t \t \t机器人:ellipsize = “字幕” \t \t \t机器人:marqueeRepeatLimit = “marquee_forever” \t \t \t机器人:scrollHorizo​​ntally = “真” \t \t \t机器人:可调焦= “真” \t \t \t机器人:focusableInTouchMode = “真” \t \t \t的android:文本= “TEXT比认为长”' – 2011-11-29 21:23:38

+1

为了增加hooraygradschool的评论:价值'layout_width'和'layout_height'可以是任何你想要的(不一定是'fill_parent'),你可以指定'android:singleLine =“true”'而不是'android:lines =“1”' ,而'android:scrollHorizo​​ntally =“true”'不是必需的(没有它,文字对我来说运行良好)。 – 2012-01-04 16:09:06

23

这些属性必须包含在textview标记中以允许滚动。

其他一切都是可选的。

android:focusable="true" 
android:focusableInTouchMode="true" 
android:layout_width="fill_parent" 
android:ellipsize="marquee" 
+0

属性'android:layout_width =“fill_parent”'对于文本滚动不是必需的(尽管您确实需要设置TextView的宽度)。你应该考虑使用以下两个属性:'android:singleLine =“true”'和'android:marqueeRepeatLimit =“marquee_forever”'。 – 2012-01-04 16:12:57

+4

请注意,有时需要根据Shardul的回答在代码中调用“TextView.setSelected(true)”(尽管设置了所有必要的属性),所以如果选取框动画无效,请尝试一下。 – 2012-01-26 14:01:33

+0

Adil Hussain,谢谢! – 2013-01-24 09:03:35

78
android:singleLine="true" 
android:ellipsize="marquee" 

是唯一必需的属性,甚至滚动与layout_width=0dp

这里定义layout_weight作品是一些示例代码:

<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" 
      /> 

但是,最重要的是隐含或明确地TextView应该被选中

你可以这样做:

TextView txtView=(TextView) findViewById(R.id.scroller); 
txtView.setSelected(true); 
+1

而不是以编程方式将TextView'selected'设置为true,您可以在xml布局文件中将“android:focusable”和“android:focusableInTouchMode”指定为true。 – 2012-01-04 15:59:32

+1

对我不起作用:( – Shardul 2012-01-05 05:08:39

+12

刚刚遇到这种情况,尽管设置了所有必需的xml属性('singleLine','ellipsize','marqueeRepeatLimit','focusable','focusableInTouchMode'),TextView字幕动画无法正常工作)并调用'TextView.setSelected(true)'做了诡计这么大非常感谢您指出这一点!! – 2012-01-26 13:57:13

19

我面临同样的问题,这个讨论帮助我,我只是这行替换该行

android:maxLines="1" 

在XML

android:singleLine="true" 

它的工作原理 该行txtView.setSelected(true); 是一个也在我的活动中。

+1

谢谢你,你节省了我的时间。 – sandeepmaaram 2016-08-27 04:26:20

+4

'android:singleLine =“true”'已被弃用。 – CopsOnRoad 2017-03-18 09:12:06

+0

与'android:maxLines =“1”一起工作https://stackoverflow.com/a/46360549/5381331 – 2017-09-22 09:15:59

9

工作代码:

<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" /> 
+0

是的工作 – Noman 2013-03-27 14:23:05

3
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; 
    } 

} 

此代码的工作正常,但如果乌拉圭回合屏幕尺寸不填补这一文本也不会移动尝试palcing文本的空白年底

2

是,对于TextView,marquee_forever也适用于的固定宽度。 (例如。机器人:layout_width = “120dp”)

必然选择所需的属性是:

  1. 机器人:可调焦= “真”
  2. 机器人:focusableInTouchMode = “真”
  3. 机器人:单线=”真正的“/ /如果它缺少文字出现在多行。

工作代码:

<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!" /> 
+1

'android:scrollHorizo​​ntally =“true”'也是必需的 – DevZer0 2013-08-10 04:21:03

2

只要把这些参数在你的TextView。它的工作原理:)

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

`

5

只需添加上面说的那些:

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

及!你必须在你的java代码中使用TextView.setSelected(true)。

在本文中,选框与某些人不起作用的原因如果您有一个带有EditText(这是一个输入)的输入窗体,EditText将是默认情况下在窗体中具有焦点和选择的窗体。现在,如果你强制通过TextView.setSelected(true),TextView最终会做选取框,无论如何。

因此,整个想法是使TextView窗口小部件聚焦和选择,使选框工作。

3

在代码中使用以下行:

TextView.setSelected(true); 
12

非常简单的工作代码:

对于无限滚动文本

  <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" /> 

&你应该必须从活动

textView.setSelected(true); 
7

我正在使用minSDK = 14,并且很好奇这些变体会起什么作用。我结束了:

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

除了其他格式的东西。我不需要scrollHoriontally,focusable或focusableInTouchMode。

这组确实需要到

setSelected(true) 

一个电话我感到有趣的是,SINGLELINE据称被弃用,并建议与MAXLINES来取代它= 1,除 - 当我这样做,这种变化单独停止滚动文本。有人会希望,当singleline最终咬到灰尘,其所有目前的行为将由maxLines触发...

0

android:focusable =“true”和android:focusableInTouchMode =“true”是必不可少的....

因为我测试了所有其他人没有这些行,结果是没有选取框。当我添加这些它开始选取框..

1

我创建了一个自定义类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); 
    } 
} 

而且你可以在欲望startMarquee ..喜欢

//textView.setSelected(true); No need of Selection.. 
textview.setAlwaysMarquee(true); 
+0

我试图实现这但文字不是马克的。我将它添加为一个类,使用它代替TextView,在其上设置各种标准字幕参数,然后调用setAlwaysMarquee。 – jskrwyk 2016-04-27 05:25:59

+0

想通了。但每次循环时都会有一些暂停。任何想法如何让它不断移动而不是轻微停顿。 – jskrwyk 2016-04-27 05:41:39

0

enter image description here

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); 
} 

} 

更多参考e点击http://androiddhina.blogspot.in/2015/10/marquee-effect-in-android-textview.html

3

您必须添加这些属性是必须放于框选

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

我遇到了同样的问题。 Amith GC的答案(第一个答案被检查为已接受)是正确的,但有时textview.setSelected(true);在文本视图不能始终获得焦点时不起作用。所以,为了确保TextView Marquee能够正常工作,我必须使用自定义的TextView。

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; 
    } 
} 

然后,你可以使用自定义的TextView作为布局滚动文本视图.xml文件是这样的:

<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"/> 

注:在上面的代码片断com.example.myapplication被一个示例软件包名称,应由您自己的软件包名称替换。

希望这会帮助你。干杯!

+0

这真是太棒了! – PrashanD 2016-06-13 10:25:14

0

大部分的答案是相同的,
也注意到,在某些情况下,选取框而不逢低为容器宽度的确定将无法正常工作。
例如,如果你在父容器

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

字幕可能无法正常使用重量。

2
<TextView 
    android:ellipsize="marquee" 
    android:singleLine="true" 
    .../> 

必须在代码中调用

textView.setSelected(true); 
1

有自己的滚动速度和灵活性来定制字幕属性,使用以下命令:

<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" /> 

中的活动:

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); 
    } 
6

我已经坤e通过textview选框不工作的情况。但按照这一点,我相信它会起作用。 :)

<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"/> 

并以编程方式添加这两行...

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

tvMarquee.setSelected(真)需要柜面如果视图的任何一个是否已经集中,并会的setSelected使其工作。 不需要使用。

android:singleLine="true" 

它已弃用,上述代码有效。

+0

如何更改滚动文字的速度? – 2018-02-19 05:50:45