Android学习笔记十四:Android TextView的append方法与滚动条同时使用

一、在Android,一个单独的TextView是无法滚动的,需要放在一个ScrollView中。

ScrollView提供了一系列的函数,其中fullScroll用来实现FOCUS_UP和FOCUS_DOWN键的功能,也就是滚动到顶部和底部。

如果在TextView的append后面马上调用fullScroll,会发现无法滚动到真正的底部,这是因为Android下很多函数都是基于消息的,用消息队列来保证同步,所以函数调用多数是异步操作的。

有消息队列是异步的,消息队列先滚动到底部,然后textview的append方法显示。所以无法正确滚动到底部。

解决办法:

final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
        if (scrollView != null) {
            scrollView.post(new Runnable() {
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
        });
        }

二、listview与滚动条一起使用,禁止listview的滚动,使用滚动条滚动到listview的底部把上面代码run里面那句换为这个scrollView.scrollTo(0, mlistViewList.getHeight());

三、listview内部高度计算函数

当listview与垂直滚动条一起使用时候,如果只用外部scrollView,而不使用listview滚动。需要下面的函数来计算listview当前高度。

public static void ReCalListViewHeightBasedOnChildren(ListView listView) {
        if (listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) return;
        int nTotalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            nTotalHeight += listItem.getMeasuredHeight();
        }
        
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = nTotalHeight + (listView.getDividerHeight()*(listAdapter.getCount()-1));
}

PS:对于APP安全检测一般我都会用:www.ineice.com

最近在开发项目中遇到一个问题,布局高度在某些国产酷派小屏幕手机上高度不够全部显示,于是使用了ScrollView嵌套LinearLayout,但问题又出现了,在大屏幕手机如三星note3手机上下面会留白,问题的解决办法是在第一层LinearLayout里面嵌套多个LinearLayout,最重要的是将ScrollView中android:fillViewport设置为true。

当ScrollView里的元素想填满ScrollView时,使用"fill_parent"是不管用的,必需为ScrollView设置:android:fillViewport="true"。

  

 

   当ScrollView没有fillVeewport=“true”时, 里面的元素(比如LinearLayout)会按照wrap_content来计算(不论它是否设了"fill_parent"),而如果LinearLayout的元素设置了fill_parent,那么也是不管用的,因为LinearLayout依赖里面的元素,而里面的元素又依赖LinearLayout,这样自相矛盾.所以里面元素设置了fill_parent,也会当做wrap_content来计算.


我用上面的方法后,滚动效果没有,但是确实是满屏了,我的解决方法是在SoralView里面加android:layout_weight="1",XML代码如下:


android:id="@+id/my_mainTableRow4"
    android:layout_width="match_parent"
    android:layout_weight="1">


    <ScrollView
        android:id="@+id/my_mainTableRow4_ScrollView"
        android:scrollbars="vertical"
        android:fadeScrollbars="false"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/my_activity_main_TextView"
            android:gravity="left"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:singleLine="false"
            android:text="">
        </TextView>
    </ScrollView>
</TableRow>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值