Android ScrollView fillViewport не работает

У меня есть простой макет с именем вверху и кнопкой, которую я хочу разместить внизу экрана или за ним, если я добавлю больше элементов.

Итак, я использую ScrollView с LinearLayout следующим образом:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".ItemDetailActivity" >

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

    <!-- Name -->

    <RelativeLayout
        android:id="@+id/top_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" >

        <TextView
            android:id="@+id/name_label"
            style="@style/Header_Label_TextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/name" />

        <TextView
            android:id="@+id/name_value"
            style="@style/Value_Label_TextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/name_label" />
    </RelativeLayout>

    <!-- button -->

    <RelativeLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <Button
            android:id="@+id/add_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10sp"
            android:onClick="editItem"
            android:text="@string/button_edit" />
    </RelativeLayout>

    <requestFocus />
</LinearLayout>

This is what I get:

Результат макета XML

Как сделать так, чтобы кнопка появлялась внизу экрана или за его пределами. При поиске в Интернете большинство ответов должны были установить `android:fillViewport="true", но это не помогает. Что я делаю не так?




Ответы (4)


Измените макет кнопки следующим образом:

 <RelativeLayout
    android:id="@+id/ButtonLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom" >

    <Button
        android:id="@+id/add_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:onClick="editItem"
        android:layout_alignParentBottom="true"
        android:text="@string/button_edit" />
</RelativeLayout>
person user755    schedule 13.09.2013
comment
Да, это работает. Я применял этот атрибут к относительному макету, и он не работал. Кажется, я понимаю, что сейчас происходит! - person rgamber; 13.09.2013

Это должно работать -

  1. Используйте Relative layout в качестве дочернего элемента для представления прокрутки.
  2. Установите layout_alignParentBottom = true для кнопки.

Пример XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".ItemDetailActivity" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Name -->

    <RelativeLayout
        android:id="@+id/top_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" >

        <TextView
            android:id="@+id/name_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Name" />

        <TextView
            android:id="@+id/name_value"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/name_label" />
    </RelativeLayout>

    <!-- button -->

    <RelativeLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/add_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10sp"
            android:onClick="editItem"
            android:text="Edit" />
    </RelativeLayout>
</RelativeLayout>

</ScrollView>
person Anukool    schedule 13.09.2013
comment
Если виртуальная клавиатура появится в фокусе EditView, эта раскладка внизу также поднимется, поскольку вы указали ей выравнивание по низу. Поэтому просто поместите EditView в свой макет и протестируйте его на устройстве с небольшим разрешением. onConfigChange, который изменяет размер экрана для клавиатуры, также не будет работать. Вы можете добавить вид разделителя ниже предпоследнего элемента относительно этого вида. - person Abhinav Saxena; 19.03.2015

В представлении прокрутки используйте RelativeLayout вместо LinearLayout и установите свойство нижнего вида.

android:layout_alignParentBottom="true"
person Krunal Shah    schedule 26.04.2016

Если вы используете LinearLayout (который более эффективен, чем RelativeLayout), вы должны установить android:gravity="bottom" и android:layout_height="match_parent" следующим образом:

<LinearLayout
    android:id="@+id/ButtonLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom" >

    <Button
        android:id="@+id/add_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:onClick="editItem"
        android:text="@string/button_edit" />
</LinearLayout>

Ссылка: https://demonuts.com/android-fillviewport/

person Aba    schedule 27.01.2020