1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android 自动上下翻滚 如何让Android TextView自动向下滚动到最后?

android 自动上下翻滚 如何让Android TextView自动向下滚动到最后?

时间:2018-08-25 01:13:45

相关推荐

android 自动上下翻滚 如何让Android TextView自动向下滚动到最后?

我有一个TextView,其内容从文本文件中复制。现在每次将文本文件的内容加载到TextView中时,我都希望它自动向下滚动到最后。

这是我的布局XML文件的部分内容:

android:id="@+id/scroller"

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:layout_above="@+id/command"

android:layout_alignParentLeft="true"

android:layout_below="@+id/header"

android:fillViewport="true" >

android:id="@+id/output"

android:layout_width="fill_parent"

android:layout_height="132dp"

android:bufferType="spannable"

android:editable="false"

android:enabled="false"

android:focusable="true"

android:focusableInTouchMode="false"

android:freezesText="true"

android:inputType="textMultiLine"

android:isScrollContainer="true"

android:scrollbars="vertical"

android:text="@string/output" >

这就是这个函数的样子:

public void displayOutput()

{

File sdcard = Environment.getExternalStorageDirectory();

File file = new File(sdcard,"/Android/data/terminalemulatorlog.txt");

StringBuilder text = new StringBuilder();

try

{

BufferedReader br = new BufferedReader(new FileReader(file));

String line;

while ((line = br.readLine()) != null)

{

text.append(line);

text.append('\n');

}

}

catch (IOException e)

{

Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();

}

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

output.setText(text);

((ScrollView) findViewById(R.id.scroller)).post(new Runnable()

{

public void run()

{

((ScrollView) findViewById(R.id.scroller)).fullScroll(View.FOCUS_DOWN);

}

});

}现在我发现here的部分解决方案。

因此,最后一行代码说:

((ScrollView) findViewById(R.id.scroller)).post(new Runnable()

{

public void run()

{

((ScrollView) findViewById(R.id.scroller)).fullScroll(View.FOCUS_DOWN);

}

});但是这只在第一次加载文本文件时起作用。我如何始终使TextView向下滚动到最后?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。