Preventing an Android ListView from taking up almost all of the screen height
I am trying to setup an activity in Android that has 50% of the screen hight for a ScrollView and the other 50% for a ListView.
To do this I have set the weight of each to "1".
Like This:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip">
<ScrollView android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<TableLayout android:id="@+id/my_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow android:padding="5dip"
开发者_Go百科 android:id="@+id/fault_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/table_title"
android:paddingRight="20dip"/>
</TableRow>
<!-- More rows are added at run time. -->
</TableLayout>
</ScrollView>
<ListView android:id="@+id/comments"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"></ListView>
</LinearLayout>
When both the ScrollView and the ListView have too much content to display without scrolling this works perfectly.
When the ScrollView only needs 20% of the screen and the ListView needs 70% the ListView grows to 70% so it can display without scrolling. This is not what I expected to happen but is good.
The problem I have is when the ListView has so much data that it would need the whole screen to display AND the Scroll view only 1 or 2 rows of data in it. In this case the ListView grows to take up almost the full height and leaves space equivalent to about one line of text for the scroll view.
I can't seem to get my head around how to reserve a certain proportion of the screen for the ScrollView no matter how big the ListView gets.
The amount of space the ScrollView can grab seems directly proportional to the number of rows in it's child table. When this is only 1 the scroll view is only about half a cm in height.
What about changing android:layout_height
for to be a minimum value instead of 0dip
?
UPDATED: As per the comments below the solution was to set the outer LinearLayout to have height "fill_parent"
Use these parameters in the ListView section
android:layout_height="0dp"
android:layout_weight="1"
精彩评论