请帮看看:为什么已设置: android:layout_height="match_parent" 但ListView还是不会自动充满足ScrollView ?

ynduanlian 2014-04-03 08:34:17
代码如下,ScrollView已经填充了屏幕上空余的部分,但是ListView却只是显示两行Item的高度,不能填充满ScrollView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btnStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1" />
<TextView
android:id="@+id/textView2"
android:padding="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text=" 至 "
android:layout_weight="0.00" />
<Button
android:id="@+id/btnEndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1" />
</LinearLayout>

<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:layout_weight="0.11" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="收入合计:" />

<TextView
android:id="@+id/lblIncome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#0000ff"
android:text="" />

</TableRow>

<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="支出合计:" />
<TextView
android:id="@+id/lblPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text="" />
</TableRow>
</TableLayout>

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2">

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >

</ListView>

</ScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.00" >

<Button
android:id="@+id/btnAddIncome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="记收入" />

<Button
android:id="@+id/btnAddPay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="记支出" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分享" />
</LinearLayout>

</LinearLayout>
请问要如何修改?
...全文
1502 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
huang0517 2016-07-27
  • 打赏
  • 举报
回复
自己写代码设置ListView的高度 我这个方法比较简单 //计算listView的高度 public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }
映映 2014-04-03
  • 打赏
  • 举报
回复
ListView 必须设定确定高度,不然套到ScrollView中,就会出现这种问题
triplesky001 2014-04-03
  • 打赏
  • 举报
回复
准备给你贴代码,发现5楼已经给了,完全可用
  • 打赏
  • 举报
回复
如楼上所说,ListView嵌入到ScrollView里,情况比较特殊。高度要算过,但你ScrollView里只有一个ListView的话,高度算好了,ScrollView都可以不需要了
网络咖啡 2014-04-03
  • 打赏
  • 举报
回复
ListView嵌入到ScrollView里面需要写代码设置ListView的高度 俺贡献一个方法你试试: /** * 设置列表框高度,解决滚屏问题 * @param listView 列表对象 * @param addHeight 增加的调节高度,默认是0 */ public static void initListViewHeight(ListView listView,int addHeight) { Adapter adapter = listView.getAdapter(); if (adapter == null || adapter.getCount() == 0) { ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = 0; listView.setLayoutParams(params); return; } int totalHeight = 0; for (int i = 0; i < adapter.getCount(); i++) { View listItem = adapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1)); params.height += 20 + addHeight; listView.setLayoutParams(params); }
ynduanlian 2014-04-03
  • 打赏
  • 举报
回复
引用 1 楼 heaimnmn 的回复:
改成fill-parent呗
好象没区别吧
ynduanlian 2014-04-03
  • 打赏
  • 举报
回复
引用 2 楼 u010449335 的回复:
<ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" > </ListView> </ScrollView> 这里面只有一个ListView为何要在外面嵌套一个ScroolView呢?
我是想实现ListView如果条目很多,可以在一屏幕的中部区域上下滚动。类似网页中部有一个IFrame这种效果,不知是否可行?
苏十五 2014-04-03
  • 打赏
  • 举报
回复
<ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" > </ListView> </ScrollView> 这里面只有一个ListView为何要在外面嵌套一个ScroolView呢?
哎,真难 2014-04-03
  • 打赏
  • 举报
回复
改成fill-parent呗
Vermouth_xi 2014-04-03
  • 打赏
  • 举报
回复
去掉ScrollView,listView也有滚动条的效果啊
ynduanlian 2014-04-03
  • 打赏
  • 举报
回复
请问,只能给ListView一个非常确定的高度吗?

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧