2012-06-28 27 views
1

我試圖使用字幕和它不工作, 這是我的代碼...任何人都可以看到這個問題?TextView字幕不工作在android

<TextView 
     android:id="@+id/lblTitle" 

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

     android:layout_width="140dp" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="15dp" 
     android:gravity="center" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:text="Book Title" 
     android:textColor="#FFFFFF" 
     android:textSize="12dp" /> 

我在運行時設置了這個textview的文本。 我使用此鏈接代碼 TextView Marquee not working

+0

可以與一些比「書名」大文字試試? –

+0

是的,我嘗試它,但它沒有完成.. – Youddh

回答

12

只要做到這樣,在烏爾活動爲我們的代碼是確定的,對於簡單的TextView選取框

TextView textview=(TextView)findViewById(R.id.lblTitle); 
textview.setSelected(true); 

和如果u想在適配器比支票使用選框中的ListView回答於Marquee in listview

+0

是的,你做對了。 – Youddh

+0

這是解決方案。其他人不工作。該視圖必須設置爲使選取框工作。注意選擇不同於重點。 –

+0

它正在工作,我很感謝 –

3

此代碼爲您工作。

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

謝謝。

+0

是的,我嘗試過,但我不是沒有什麼是我的代碼問題.. – Youddh

+0

我測試它,它工作。另一種認爲沒有日誌文本(測試寬度必須大於手機寬度)它不wotking,也需要Android 1.5及以上 –

4
working on my phone 

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/lblTitle" 

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

      android:layout_width="140dp" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="15dp" 
      android:gravity="center" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp" 
      android:text="Book Title sad ds sdas das asdas das asd asd sad as" 
      android:textColor="#FFFFFF" 
      android:textSize="12dp" /> 
    </LinearLayout> 

enter image description here

+1

是的,我創建新的項目和它的工作,但在我的項目中,我在自定義列表視圖中使用此文本視圖,不工作。 – Youddh

+0

g8 ..所以你可以更新你完整的佈局,以瞭解佈局以找到問題.... –

+0

你累了tv.setSelected(true); ?在你的大布局 –

1

此代碼工程

<TextView 
android:text="A very long book title" 
android:id="@+id/book_title" 
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"> 
1

我遇到了同樣的問題。 汗的答案是正確的,但有時 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

我的問題唯一正確的解決方案,謝謝 –

0

的這行代碼的工作100%,只要把你的TextView XML代碼中這些行,它會開始工作

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