Scrollview填充屏幕的剩余部分

时间:2018-03-08 11:06:39

标签: java android

我正在尝试在活动底部设置2个按钮,其中滚动视图显示列表。当前它只显示一行,而不是显示在屏幕上可以容纳的行数减去底部的按钮。有没有办法在不使用DP的情况下扩展Scrollview / Listview(因为它并不总是与不同的屏幕尺寸相关)。

enter image description here

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <ListView
        android:id="@+id/user_event_table"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="left|bottom"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/back_button"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center"
            android:text="back" />

        <Button
            android:id="@+id/add_button"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center"
            android:text="add Event" />
    </LinearLayout>
</LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

不要将ListView嵌套在ScrollView中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

    <LinearLayout
        android:id="@+id/button_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <Button
            android:id="@+id/back_button"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:text="back"/>

        <Button
            android:id="@+id/add_button"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:text="add Event"/>
    </LinearLayout>

    <ListView
        android:id="@+id/user_event_table"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/button_layout"
        android:layout_alignParentTop="true"/>

</RelativeLayout>

答案 1 :(得分:0)

这是解决方案

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
        <ListView
            android:id="@+id/user_event_table"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ListView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left|bottom"
        android:orientation="vertical">

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

            <Button
                android:id="@+id/back_button"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center"
                android:text="back" />

            <Button
                android:id="@+id/add_button"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center"
                android:text="add Event" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>