2010-07-26 73 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