在第一行的TextView滚动 [英] textView scroll at first line

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

问题描述

我想在文本的某些部分,我知道我的滚动TextView的,并显示在上面。这是我的滚动视图和TextView的:

i want to scroll my TextView at some portion of text that i know, and show it on top. this is my scrollView and TextView:

<ScrollView   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:scrollbars="vertical"
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">     

    <TextView 
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"/>  
</ScrollView>

我的文字是字符串数组:

My text is an array of strings:

ScrollView scroll= (ScrollView) getActivity().findViewById(R.id.scroll);
TextView text = (TextView) getActivity().findViewById(R.id.text);
String book ="";
for(String line: lines){
  book += line; 
}
text.setText(book);

这是当我想要滚动:

String find = book[myindex];
int go = text.indexOf(find);
makeScroll(go);

这是我的可运行:

    private void makeScroll(final int go){
        scrollRegole.post(new Runnable() {
            public void run() {
                  scrollRegole.scrollTo(0, go);
            }
        });
    }

这是不行的,为什么呢?谢谢!

this is not work, why? thanks!

推荐答案

这是我如何解决我的问题:
这是我的新滚动型:

this is how i resolved my problem: this is my new ScrollView:

    <ScrollView 
        android:id="@+id/scroll"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:layout_weight="3" >

        <LinearLayout
            android:id="@+id/containerTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>      
    </ScrollView>

在这里,我增加了许多的TextView到我的容器:

here i add many TextView into my container:

if(scroll == null){
   scroll = (ScrollView) getActivity().findViewById(R.id.scroll);
}

if(layout == null)
       layout = (LinearLayout) getActivity().findViewById(R.id.containerTextView);

layout.removeAllViews();

for(String line: lines){
   TextView text = new TextView(getActivity());
   text.setText(line);  
   text.setTextSize(...));          
   layout.addView(text);
}

这是当我想要滚动:

private void calculateScroll(final LinearLayout layout, final int indexLine){

    layout.addOnLayoutChangeListener(new OnLayoutChangeListener(){

        @Override
        public void onLayoutChange(View paramView, int paramInt1,
                int paramInt2, int paramInt3, int paramInt4, int paramInt5,
                int paramInt6, int paramInt7, int paramInt8) {

            int y = 0;
            for(int x=0; x< indexLine; x++){    
                y += layout.getChildAt(x).getHeight();
            }

            makeScroll(y);
        }

    });
}

private void makeScroll(final int go){
    scrollRegole.post(new Runnable() {
        public void run() {
            scroll.scrollTo(0, go);
        }
    });
}

在这里,如果我想显示行[3](对于exsampe ..)我知道正确的高度。我现在的问题是TextView中之间移除空间..但它的工程!

here if i want to show lines[3] (for exsampe..) i know the correct height. My problem now is remove space between TextView.. but it works!

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

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