2016-06-16 21 views
0

Ich habe versucht, überall zu suchen, aber ich kann nicht scheinen, das herauszufinden. Grundsätzlich habe ich ein Layout mit Scrollview und Elementen darin. Ich möchte eine Schaltfläche in der Bildlaufleiste einfügen, die sich am Ende/unten befindet und Sie können sie nur sehen, wenn Sie den Bildlauf ganz nach unten durchführen. Ich habe versucht, es so zu tun:Wie man den Knopf auf den Boden von Scrollview setzt

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:orientation="vertical"> 

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

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

     <android.support.v7.widget.Toolbar 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/toolbar_review" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
      app:title="Review Your Massage"> 

     </android.support.v7.widget.Toolbar> 


     <include layout="@layout/card_review"/> 

    </LinearLayout> 


    <Button 
     android:id="@+id/buttonReview" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:layout_alignParentBottom="true" 
     android:background="@color/colorAccent" 
     android:text="@string/book" 
     android:layout_below="@+id/topcontainer" 
     /> 
</RelativeLayout> 
</ScrollView> 

Der Knopf ist in der Tat am Ende, aber es gibt Raum darunter. Wenn ich die App auf einem Telefon mit größerer Auflösung verwende, befindet sich die Schaltfläche fast in der Mitte des Bildschirms. Wie kann ich es so machen, egal in welcher Auflösung es immer unten bleiben wird?

Hier ist ein Screenshot von dem, was es sieht aus wie im Moment: enter image description here

+0

android entfernen: layout_alignParentBottom = "true" – Pavya

+0

Set Höhe von Scroll = "match_parent" & nicht "wrap_content" gleiche gilt für RelativeLayout –

+0

und Add unter . Relatives Layout entfernen – Pavya

Antwort

4

Mit android:layout_height="match_parent" und android:fillViewport="true" die ScrollView nutzen alle zur Verfügung stehenden Raum, auch wenn die Kinder sind kleiner.

Die Space hat eine android:layout_weight="1", um den zusätzlichen Platz zu erhalten, so dass die Schaltfläche immer an der Unterseite ist.

Ein explanation by Romain Guy mit mehr Details.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      android:fillViewport="true" 
      android:orientation="vertical"> 

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

      <android.support.v7.widget.Toolbar 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/toolbar_review" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@color/colorPrimary" 
       android:minHeight="?attr/actionBarSize" 
       android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
       app:title="Review Your Massage"> 

      </android.support.v7.widget.Toolbar> 

      <include layout="@layout/card_review"/> 

      <android.support.v4.widget.Space 
       android:layout_width="match_parent" 
       android:layout_weight="1" 
       android:layout_height="0dp" /> 

      <Button 
       android:id="@+id/buttonReview" 
       android:layout_width="match_parent" 
       android:layout_height="60dp" 
       android:background="@color/colorAccent" 
       android:text="@string/book" /> 
     </LinearLayout> 

</ScrollView> 
+0

Vielen Dank für die Erklärung, aber wenn ich es auf Ihre Weise mache, verschwindet die Schaltfläche. – jlively

+0

für beide Fälle? (mit einem Inhalt kleiner/größer als der Bildschirm) – bwt

+0

Ja, für beide Fälle. Die Schaltfläche wird nicht angezeigt. – jlively

0

versuchen, diese

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:fillViewport="true" 
    android:orientation="vertical"> 

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

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

    <android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/toolbar_review" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:title="Review Your Massage"> 

    </android.support.v7.widget.Toolbar> 


    <include layout="@layout/card_review"/> 

</LinearLayout> 


<Button 
    android:id="@+id/buttonReview" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:background="@color/colorAccent" 
    android:text="@string/book" 
    /> 

</RelativeLayout> 
</ScrollView> 
+0

Wenn ich das an nehme, sagen wir Nexus 6P, funktioniert es, der Knopf ist unten. Aber wenn ich es auf Nexus 4 verwende, ist der Inhalt nicht scrollbar und die Schaltfläche wird immer angezeigt. Ich brauche es nur zu sehen, wenn man runterscrollt. – jlively

+0

Sie können versuchen, die Schaltfläche zu verbergen und sichtbar zu machen, wenn der scrollView den unteren Rand erreicht. Es gibt einen Link, der erklärt, wie man erkennt, wenn scrollView den Boden erreicht. http://stackoverflow.com/questions/10316743/detect-end-of-scrollview –