2013-03-01 13 views
8

Jestem nowy w rozwoju Androida i mam następujący problem. Muszę użyć FrameLayout wewnątrz ScrollView, aby przewinąć. Napisałem to:FrameLayout wewnątrz ScrollView

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="#00ff00"> 

    <FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="500dp" 
     android:background="#ff0000"> 
    </FrameLayout> 
    </ScrollView> 

Ale to nie działa. Próbowałem w RelativeLayout i działało, ale muszę użyć dla FrameLayout. Jakieś pomysły?

Odpowiedz

4

Próbowałem wielu wariantów rozwiązania tego problemu, w tym odpowiedzi tutaj, ale nikt nie był pomocny. ScrollView zawsze owija FrameLayout, ale wszystko jest normalne ze RelativeLayout, LinearLayout ...

Znalazłem jedno rozwiązanie do tego - aby ustawić minimalną wysokość FrameLayout. Możesz ustawić dynamicznie minimalną wysokość metodą setMinimumHeight().

2

Twój xml jest bardzo dobry, jedyne co widzę to: jeśli rodzic ScrollLayout jest większy niż 500dp, nie przewinie się.

Rzeczywiście, przewijanie jest używane tylko wtedy, gdy zawartość jest większa niż widok przewijania.

tutaj można ustawić swój wzrost zawartości do 500dip i Scrollview do „match_parent” jeśli rodzic tego Scrollview wziąć cały ekran, na ekranie wysokość 800dip (na przykład)

=> zwój jest po prostu nie potrzebne.

2

ustawienie Spróbuj layout_height do „wrap_content” w FrameLayout

<FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ff0000"> 

będzie przewijać gdy FrameLayout będzie wyższa niż na granicy ekranu. Lub jeśli chcesz ustalić wysokość przewijalnego obszaru, ustaw wysokość na 500dp na ScrollView. To zależy od tego, czego chcesz, ale nigdy nie ustawiaj stałej wysokości na dziecku ScrollView. Wysokość ScrollView powinna zawsze wynosić wrap_content.

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="500dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="#00ff00"> 

    <FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ff0000"> 
    </FrameLayout> 
</ScrollView> 
1

Aby uzyskać FrameLayout wewnątrz Scrollview, to zrobić:

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayoutHeader1" 
    android:layout_centerHorizontal="true" > 

    <LinearLayout 
     android:id="@+id/LinearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
     <FrameLayout 
      android:id="@+id/FrameLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="1440dp" 
      > 
     </FrameLayout> 

    </LinearLayout> 
</ScrollView> 
13

androida: fillViewport = "true" rozwiązuje ten problem.

android: fillViewport, Określa, czy przewijanie powinno rozciągać zawartość, aby wypełnić rzutnię

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

    <FrameLayout 
     android:id="@+id/frameLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </FrameLayout> 
    </ScrollView> 
Powiązane problemy