2011-03-13 6 views
2

TextView를 스크롤하여 텍스트의 특정 위치를 볼 수있게하려고합니다. 어떻게해야합니까? 나는 bringPointIntoView (int offset)를 시도했지만 성공하지는 못했다.TextView를 텍스트 위치로 스크롤

소스 코드 : 당신이 더 나은 솔루션이있는 경우

public static void bringPointIntoView (TextView textView, 
    ScrollView scrollView, int offset) 
    { 
    int line = textView.getLayout().getLineForOffset (offset); 
    int y = (int) ((line + 0.5) * textView.getLineHeight()); 
    scrollView.smoothScrollTo (0, y - scrollView.getHeight()/2); 
    } 

주저하지 마십시오 같은 문제를 가지고 사람들을 위해

public class TextScrollActivity extends Activity { 
    public void onCreate (final Bundle savedInstanceState) { 
    super.onCreate (savedInstanceState); 
    final int position = 500; 
    final TextView textView = new TextView (this); 
    final ScrollView scrollView = new ScrollView (this); 
    scrollView.addView (textView); 
    Button button = new Button (this); 
    button.setText ("Scroll to " + position); 
    LinearLayout layout = new LinearLayout (this); 
    layout.setOrientation (LinearLayout.VERTICAL); 
    layout.addView (scrollView, 
    new LayoutParams (LayoutParams.FILL_PARENT, 200)); 
    layout.addView (button, new LayoutParams (LayoutParams.FILL_PARENT, 
    LayoutParams.WRAP_CONTENT)); 
    StringBuilder builder = new StringBuilder(); 
    for (int i = 0; i < 1000; i++) 
     builder.append (String.format ("[ %05d ] ", i)); 
    textView.setText (builder); 
    setContentView (layout); 
    button.setOnClickListener (new OnClickListener() { 
     public void onClick (View v) { 
     System.out.println (textView.bringPointIntoView (position * 10)); 
     // scrollView.scrollTo (0, position * 10); // no 
     } 
    }); 
    } 
} 
+0

ScrollView를 제거하면'bringPointIntoView' 메서드가 작동하는 것처럼 보이지만 지금은 TextView를 스크롤 할 수 없습니다 ... 어떻게 제발 해결할 수 있습니까? – Patrick

답변

3

, 나는 마침내 bringPointIntoView 내 자신의 구현을했다.

1

텍스트보기에 이동 방법을 추가하면 문제가 해결됩니까?

textView.setMovementMethod(new ScrollingMovementMethod()); 
+0

부분적으로 만, 그리기 문제가 있고, 플링 애니메이션이없고, 스크롤바가 없기 때문입니다. 어쨌든 고마워 :) – Patrick

1

그냥 참고로 큰 listItemslistView에 같은 문제를 가진 다른 사람을 위해, 오버로드 bringPointIntoView 대신 ScrollViewListView을 통과 대신 smoothScrollToScrollTo 방법을 사용할 수 있습니다.

관련 문제