2013-10-29 61 views
0

請看看下面的代碼文字跑馬燈不工作

<TableRow 
      android:id="@+id/tableRow13" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dp" > 

      <TextView 
       android:id="@+id/textView22" 
        android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="#ffffff" 
       android:textSize="12sp" 
       android:text="@string/r_new_event" 
       android:layout_marginLeft="5dp" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 

      <TextView 
       android:id="@+id/textView23" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Meeting with people i have never seen before in my entire life lol" 
       android:ellipsize="marquee" 
       android:maxLength="10" 
       android:singleLine="true" 
       android:fadingEdge="horizontal" 
       android:marqueeRepeatLimit="marquee_forever" 
       android:scrollHorizontally="true" 
       android:textColor="#ffffff" 
       android:textSize="12sp" 
       android:layout_marginLeft="10dp" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 

</TableRow> 

這個嘗試是框選文字,如果超過10個字母長。但不幸的是,這段代碼不起作用。我嘗試了飛蛾模擬器和Android手機。這裏有什麼問題?

更新

以下是我新的代碼,它仍然沒有工作

<TextView 
    android:id="@+id/textView23" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Meeting with people i have never seen before in my entire life lol" 
    android:ellipsize="marquee" 
    android:maxLength="10" 
    android:singleLine="true" 
    android:fadingEdge="horizontal" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:scrollHorizontally="true" 
    android:textColor="#ffffff" 
    android:textSize="12sp" 
    android:layout_marginLeft="10dp" 
    android:focusable="true" 
android:focusableInTouchMode="true" 
    android:freezesText="true" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 
+0

我認爲你必須有重點的TextView爲它選取框以及 – kabuto178

+0

檢查:http://stackoverflow.com/questions/3332924/textview-marquee-not-working和http://developer-dot-android.blogspot.in/2012/03/marquee-effect-in-textview-tutorial.html –

+0

@androiduser:令人驚訝的事實是,這也是行不通的! –

回答

2

試試這個代碼..

XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/mywidget" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:lines="1" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:marqueeRepeatLimit="marquee_forever" 
     android:scrollHorizontally="true" 
     android:textColor="#ff4500" 
     android:text="Simple application that shows how to use marquee, with a long text" /> 
</RelativeLayout> 

MainActivity

public class TextViewMarquee extends Activity { 
    private TextView tv; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv = (TextView) this.findViewById(R.id.tv); 
     tv.setSelected(true); // Set focus to the textview 
    } 
} 
+0

只有這個代碼是不夠的。佈局的寬度必須是0dp,爲了在版本4中工作 –

0

添加這些:

android:focusable="true" 
android:focusableInTouchMode="true" 
android:freezesText="true" 
+0

感謝您的回覆。我試過了,不行。請看看更新後的問題 –

+1

Check @ DineshKumar的回答。你應該添加'setSelected(true);' –

0

替換爲您的TextView下面的代碼

<TextView 
       android:id="@+id/textView23" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Meeting with people i have never seen before in my entire life lol" 
       android:ellipsize="marquee" 
        android:lines="1" 
       android:singleLine="true" 
       android:fadingEdge="horizontal" 
       android:marqueeRepeatLimit="marquee_forever" 
       android:scrollHorizontally="true" 
       android:textColor="#ffffff" 
       android:textSize="12sp" 
       android:layout_marginLeft="10dp" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:freezesText="true" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
+0

這也沒有工作 –

+0

我這邊工作 –

+0

我使用你給定的xml代碼並編輯它正在工作的代碼 –

0

使用下面的自定義的TextView /的onCreate只寫your_textView.setSelected(真);

public class MyTextView extends TextView { 

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

public MyTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public MyTextView(Context context) { 
    super(context); 
} 

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

}