Android RelativeLayout: как выравниватьParentBottom при завершении в ScrollView?

Проблема, с которой я сталкиваюсь, заключается в том, что если у меня есть представление (например, myButton) внутри RelativeLayout, установленное для выравниванияParentBottom - (в соответствии с приведенным ниже кодом), а затем оберните RelativeLayout с помощью scrollview (необходимо, чтобы содержимое быть видимым на меньших экранах или при изменении ориентации в ландшафт) - тогда, когда нижнее дно родителя НЕ отображается (например, когда оно изменено на альбомную ориентацию), myButton отображается в верхней части страницы - НЕ внизу.

Как вы можете выровнять представление в нижней части экрана и оставить его внизу, даже если нижняя часть находится ниже прокрутки?

<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="true">



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/topLayout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">



... lots of views and stuff


<Button android:layout_alignParentBottom="true" 
android:id="@+id/myButton"  
android:text="Click Me" />

</RelativeLayout>
</ScrollView>   

Ответ 1

Выньте кнопку из режима просмотра прокрутки, обмотайте как прокрутку, так и кнопку в линейной компоновке, установите высоту прокрутки в 0dip и установите ее вес в 1, не устанавливайте значение веса для кнопки, поэтому scrollview может занять все оставшееся пространство, экономя только путь в пространстве кнопок внизу.

<?xml version="1.0" encoding="utf-8"?>
<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="0dip"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

        ....lots of views...


        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="Button" />

</LinearLayout>

Ответ 2

Использование fill_parent в качестве высоты дочернего элемента ScrollView бессмысленно. Вы говорите, что RelativeLayout всегда высок, как его родитель. В этом случае ScrollView становится бесполезным! Высота RelativeLayout должна быть установлена ​​в wrap_content, и в этом случае, в зависимости от того, что содержит RelativeLayout, alignParentBottom может работать не так, как вы ожидаете. Вы должны просто использовать LinearLayout, это будет намного проще.

Ответ 3

ну, вы можете попробовать это

<LinearLayout android:id="@+id/parentLayout"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ScrollView android:id="@+id/mainScrollView"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_weight="1" xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillViewport="true">



        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/topLayout" android:layout_width="fill_parent"
            android:layout_height="fill_parent">



            ... lots of views and stuff


            <Button android:layout_alignParentBottom="true" android:id="@+id/myButton"
                android:text="Click Me" />

        </RelativeLayout>
    </ScrollView>
</LinearLayout>

Ответ 4

Немного поздно, но самым простым решением является добавление

android:below="@id/lots_of_stuffs"

Вот так:

<ScrollView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainScrollView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">

   <RelativeLayout
     android:id="@+id/topLayout"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">


     <LinearLayout
       android:id="@+id/lots_of_stuffs" >

       ... lots of views and stuff

     </LinearLayout>

    <Button android:layout_alignParentBottom="true" 
      android:id="@+id/myButton"  
      android:text="Click Me"
      android:below="@id/lots_of_stuffs" /> <!-- Here is the "hack" -->

   </RelativeLayout>
</ScrollView>