ScrollView只能有一个子控件

ScrollView只能添加一个子控件 ,如果添加了多个子控件,则会出现“ScrollView can host only one direct child”异常
解决办法:子控件外面再套一层LinearLayout,注意只能是Linearlayout,布局中不能出现RelativeLayout

scrollview内部组件android:layout_height="fill_parent"无效的解决办法
解决办法:
需要设置scrollview的fillViewport属性为"true"才能使其子组件可以扩展

在ViewPager 外面嵌套ScrollView 时,发现ViewPager中的元素显示不出来。
解决办法:
在ScrollView节点指定android:fillviewport="true"

ScrollView嵌套ListView,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);  


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值