滚动的TextView文本位置 [英] Scroll TextView to text position

查看:185
本文介绍了滚动的TextView文本位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要滚动我的TextView,使文本可见的特定位置。我该怎么办呢?我想bringPointIntoView(INT偏移),但没有成功。

来源$ C ​​$ C:

 公共类TextScrollActivity延伸活动{
  公共无效的onCreate(最终捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    最终诠释位置= 500;
    最后的TextView TextView的=新的TextView(本);
    最后的滚动型滚动视图=新的滚动型(本);
    scrollView.addView(TextView的);
    Button按钮=新的按钮(这一点);
    button.setText(滚动到+位置);
    的LinearLayout布局=新的LinearLayout(本);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(滚动视图,
    新的LayoutParams(LayoutParams.FILL_PARENT,200));
    layout.addView(按钮,新的LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));
    StringBuilder的建设者=新的StringBuilder();
    的for(int i = 0; I< 1000;我++)
      builder.append(的String.Format([%05D],I));
    textView.setText(制造商);
    的setContentView(布局);
    button.setOnClickListener(新OnClickListener(){
      公共无效的onClick(视图v){
        的System.out.println(textView.bringPointIntoView(位置* 10));
        // scrollView.scrollTo(0,位置* 10); // 没有
      }
    });
  }
}
 

解决方案

对于那些谁有同样的问题,我终于做出了我自己的实现bringPointIntoView的:

 公共静态无效bringPointIntoView(TextView的TextView的,
  滚动型滚动视图,诠释抵消)
  {
    INT线= textView.getLayout().getLineForOffset(补偿);
    INT Y =(int)的((线+ 0.5)* textView.getLineHeight());
    scrollView.smoothScrollTo(0,Y  -  scrollView.getHeight()/ 2);
  }
 

如果你有一个更好的解决办法,不要犹豫。

I want to scroll my TextView to make visible a specific position in the text. How can I do that? I tried bringPointIntoView (int offset) but without success.

Source code:

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

解决方案

For those who have the same problem, I finally made my own implementation of bringPointIntoView:

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

Don't hesitate if you have a better solution.

这篇关于滚动的TextView文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆