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向下滚动到最后?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值