2012-11-27 49 views
2

我希望按鈕中的文本滾動...所以爲此,我使用按鈕上的文本視圖。 我在佈局中設置了以下屬性。但它不起作用。 我在RelativeLayout中使用這個...任何人都可以告訴我我的代碼有什麼問題嗎?Android,文本中的水平滾動查看

<TextView 
    android:id="@+id/textView1" 
    android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
    android:layout_width="wrap_content" 
    android:singleLine="true" 
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit ="marquee_forever" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:scrollHorizontally="true" 
    android:layout_height="wrap_content"/> 

我得到了解決,我分享這個...我創建了一個類

import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class ScrollingTextView extends TextView { 

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

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

    public ScrollingTextView(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; 
    } 

} 

因爲我需要一個以上的TextView要在屏幕上滾動... 未來在layout.xml文件用這個....

<com.yourpackage.ScrollingTextView 
     android:id="@+id/TextView01" 
     android:layout_width="100dip" 
     android:layout_height="wrap_content" 
     android:ellipsize="marquee" 
     android:marqueeRepeatLimit="marquee_forever" 
     android:padding="5dip" 
     android:scrollHorizontally="true" 
     android:singleLine="true" 
     android:text="1.option" /> 

瞧....它做

+0

的可能重複[有沒有辦法讓橢圓大小=「選框」總是滾動?](http://stackoverflow.com/questions/1827751/is-there-a-way-to-make-ellipsize-marquee-always-scroll) – Sam

+0

我得到了這個問題的解決方案.... –

回答

0

如果您不需要子類TextView。

TextView 
     android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
     android:singleLine="true" 
     android:ellipsize="marquee" 
     android:marqueeRepeatLimit ="marquee_forever" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:scrollHorizontally="true" 
     android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 



public class WordExtractTest extends Activity { 

    TextView tv1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     tv1 = (TextView)findViewById(R.id.tv1); 

     loadDoc(); 
    } 

    private void loadDoc(){ 

     String s = ""; 

     for(int x=0;x<=100;x++){ 
      s += "Line: "+String.valueOf(x)+"\n"; 
     } 

     tv1.setMovementMethod(new ScrollingMovementMethod()); 

     tv1.setText(s); 

    } 
} 
0

作出這樣的擴展TextView的一類,因爲你需要滾動條上的一個以上的textveiw

import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class ScrollingTextView extends TextView { 

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

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



    public ScrollingTextView(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; 
    } 

} 

現在使用這個類,使您的文本視圖volla對其做

<com.yourPackage.ScrollingTextView 
     android:id="@+id/TextView" 
     android:layout_width="100dip" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button4" 
     android:layout_alignTop="@+id/button4" 
     android:layout_marginLeft="18dp" 
     android:ellipsize="marquee" 
     android:marqueeRepeatLimit="marquee_forever" 
     android:padding="5dip" 
     android:scrollHorizontally="true" 
     android:singleLine="true" 
     android:text="Text" 
     android:textColor="#000000" />