2011-06-06 63 views
1

我正在尋找一種讓TextView滾動的方法。我發現了這個問題的答案,使用特定的XML屬性涉及...滾動TextView重繪問題

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" android:orientation="vertical"> 
    <TextView android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:id="@+id/textview" android:text="The long text, which no way have chance to fit within screen's width" 
     android:maxLines="1" 
     android:scrollHorizontally="true" /> 
</LinearLayout> 

...一小片的源代碼

package spk.sketchbook; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.method.ScrollingMovementMethod; 
import android.view.Gravity; 
import android.widget.TextView; 

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

     TextView textView = (TextView)findViewById(R.id.textview); 
     textView.setMovementMethod(ScrollingMovementMethod.getInstance()); 
    } 
} 

...一切作品般的魅力,但直到一個試圖對齊文本中的TextView向右......

android:gravity="right|center_vertical" 

...觀察,直到用戶試圖滾動文本,它是無形的(可能滾出風景)。

不幸的是,這正是我需要做的(使右對齊的TextView可滾動)。

有什麼想法嗎?

回答

1

這是一種不同的方法:

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textview" 
     android:gravity="right|center_vertical" 
     android:text="The long text, which no way have chance to fit within screen's width" 
     android:maxLines="1"/> 
</HorizontalScrollView> 

我認爲你不需要任何代碼來完成這項工作

+1

我終於改變了TextView的進入EditText上,問題就解決了。我想,儘管如此,你的解決方案也會起作用。然而,這是我發現的第三個Android庫錯誤,這使得Android開發非常惱人。感謝幫助。 – Spook 2011-06-13 11:25:50

+0

@Spook感謝您使用'EditText'的想法,但它可以幫助我使用'setMaxLines(1)',不要使用'setSingleLine' – BNK 2016-06-02 09:25:00