Android TextView 加入滚动功能

不好的方法

安卓的TextView,一页放不下,就需要用到滚动功能,代码如下,加入ScrollView即可。

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
                
                <TextView
                    android:id="@+id/textview_first"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="0dp"
                    android:text="hello_first_fragment" />

            </ScrollView>

参考: Android设置TextView可滚动_互联网小熊猫的博客-CSDN博客_android textview 滚动

总结:这个方法虽然可以使用,但是滚动的效果很不好,PASS.

使用ScrollView和TextView共同实现

参考:Android TextView更新内容后自动滑行到最后一行_我又来瞟代码了的博客-CSDN博客

这个方法很好。能完全滚到最后。

XML代码:其实就是加一个ID即可。

            <ScrollView
                android:id="@+id/scrollview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:padding="5dp"
                android:scrollbars="vertical"
                android:fadeScrollbars="false"
                >

初始化代码:

static public TextView m_TextView_Display = null;
static public ScrollView m_ScrollView = null;


m_TextView_Display = findViewById(R.id.textview_first);
m_ScrollView = (ScrollView) findViewById(R.id.scrollview);

调用代码,在显示完后,顺便滚动一下:

                    m_TextView_Display.append(str);
                    m_ScrollView.smoothScrollTo(0, m_TextView_Display.getBottom()+100);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值