【Android-View】基于原生View的简单功能定制
16lz
2021-01-23
1. TextView
1.1 设置TextView可滚动且更新文字后自动滚动至最后一行
【方案】如下步骤
① 在TextView布局文件中给TextView加入如下属性
android:scrollbars="vertical" android:fadeScrollbars="false"
② 在Activity中的onCreate()方法中,使用setMovementMethod(MovementMethod movement)方法配置TextView的滚动方式。
TextView tv = (TextView) findViewById(R.id.tv_log);tv.setMovementMethod(ScrollingMovementMethod.getInstance());
③ 更新文字时,使用View.scrollTo(int x,int y)方法使其自动滚动到最后一行。
private void printLog(String log) { tv.append("\n" + log); // 计算偏移量 int offset = tv.getLayout().getLineTop(tv.getLineCount()) + tv.getCompoundPaddingTop() + tv.getCompoundPaddingBottom(); // 滚动到目标定位点 if (offset > tv.getHeight()) { tv.scrollTo(0, offset - tv.getHeight()); } }
【参考1】https://www.jianshu.com/p/01d9b4564908 # Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行?
【参考2】https://stackoverflow.com/questions/34248301/textview-getlinecountgetlineheight-getheight# # TextView getLineCount()*getLineHeight() != getHeight()
更多相关文章
- Android 修改app图标和名称的方法
- 【Android】adapter has changed解决方法
- android中控件点击两次才响应onclick方法
- android彻底结束进程的两种方法
- android和html交互--动态注入方法
- Android方法数超出限定的问题(multiDex,jumboMode)
- Android原生方法和Web JS互相调用-两种写法
- android 访问网络不能在主线程中进行以及在线程中操作UI的解决方
- Android Studio——Android Studio更新升级方法