■ScrollView垂向滚动视图
1.概述:用于为其他控件添加滚动条而生成垂直滚动样式效果的滚动视图。
只能包含一个控件,经常被用来包含一个布局。
2.功能:虽不能被称为真正的容器,但它能为所包裹的容器View(甚至非容器的)添加垂直滚动条。
默认情况,它只添加垂直滚动条;若同时要添加水平滚动条,就要先包裹horizontalScrollView。
3.优点:即使内容超出了屏幕大小,也可以通过滚动方式供用户浏览。
4.应用:内容比较多的布局,比如上下滚动阅读器/浏览器等。
注意:
(1)ScrollView直接包裹的控件只能为一个,而且只能为LinearLayout。
(2)scrollview内部组件android:layout_height="match_parent"无效的解决办法,
设置scrollview的fillViewport属性为"true"。
(3)外面套着ScrollView的 ViewPager的子控件显示不出来的可能解决办法,
设置scrollview的fillViewport属性为"true"。
(4)ScrollView嵌套的ListView不能完全正确地显示的可能解决方法:
(可能只需要一下第一步。)
1)自定义一个ListView,重写onMeasure方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
2)计算每个Item的高度
public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); // 计算子项View 的宽高
totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
3)listview添加适配器后或者适配器加载数据后重新设置ListView高度
listView.setAdapter(adapter);
new ListViewUtil().setListViewHeightBasedOnChildren(listView);
1.概述:用于为其他控件添加滚动条而生成垂直滚动样式效果的滚动视图。
只能包含一个控件,经常被用来包含一个布局。
2.功能:虽不能被称为真正的容器,但它能为所包裹的容器View(甚至非容器的)添加垂直滚动条。
默认情况,它只添加垂直滚动条;若同时要添加水平滚动条,就要先包裹horizontalScrollView。
3.优点:即使内容超出了屏幕大小,也可以通过滚动方式供用户浏览。
4.应用:内容比较多的布局,比如上下滚动阅读器/浏览器等。
注意:
(1)ScrollView直接包裹的控件只能为一个,而且只能为LinearLayout。
(2)scrollview内部组件android:layout_height="match_parent"无效的解决办法,
设置scrollview的fillViewport属性为"true"。
(3)外面套着ScrollView的 ViewPager的子控件显示不出来的可能解决办法,
设置scrollview的fillViewport属性为"true"。
(4)ScrollView嵌套的ListView不能完全正确地显示的可能解决方法:
(可能只需要一下第一步。)
1)自定义一个ListView,重写onMeasure方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
2)计算每个Item的高度
public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); // 计算子项View 的宽高
totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
3)listview添加适配器后或者适配器加载数据后重新设置ListView高度
listView.setAdapter(adapter);
new ListViewUtil().setListViewHeightBasedOnChildren(listView);