一、在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"。
我用上面的方法后,滚动效果没有,但是确实是满屏了,我的解决方法是在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>