2012-08-26 103 views
6

我正在爲Android設備製作應用程序,我正在嘗試在LinearLayout中使用ScrollView,但是當我嘗試執行此操作時,ScrollView將獲取ScrollView之後的所有空間和元素在LinearLayout中不起作用。Android:垂直LinearLayout的滾動視圖

例如:
如果滾動型是不是 「全」:
http://kakko76.free.fr/scroll2.jpg
如果滾動型是 「充分」:
http://kakko76.free.fr/scroll1.png

正如你可以看到按鈕disapear ...
以下是此活動的代碼:

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_marginLeft="50dp" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:orientation="vertical" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:background="@drawable/linearlayoutbackground" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/nom_des_joueurs" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_marginBottom="30dp" /> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

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

     </LinearLayout> 

    </ScrollView> 

    <Button 
     android:id="@+id/okPlayersName" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/ok" 
     android:background="@drawable/backgroundbutton" 
     android:layout_marginTop="30dp" /> 
</LinearLayout> 

在添加Linea中的元素後rLayout誰在ScrollView中。
任何解決方案?
謝謝。

回答

9

嘗試以下操作:

android:layout_height="0dp" 
android:layout_weight="1" 

這告訴你的滾動型採取一切不被其他元素所佔據的剩餘空間。

+1

正在寫相同的解決方案在同一時間:-p你更快.. –

+0

謝謝你的工作:) – kakko76

1

你有android:layout_height="wrap_content"作爲你的身高。這意味着高度與其中的內容一樣高。如果你不想讓它佔據整個地方,你可以給它重量(如上面的答案),或者你可以用dp來設置你的高度。例如:

<LinearLayout 
android:id="@+id/linearLayout1" 
android:layout_width="fill_parent" 
android:layout_marginLeft="50dp" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 
android:orientation="vertical" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:background="@drawable/linearlayoutbackground" 
android:weightSum="1.5" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight = "0.2" 
    android:text="@string/nom_des_joueurs" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_marginBottom="30dp" /> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight = "1"> 

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

    </LinearLayout> 

</ScrollView> 

<Button 
    android:id="@+id/okPlayersName" 
    android:layout_width="match_parent" 
    android:layout_height="0" 
    android:layout_weight = "0.3"> 
    android:text="@string/ok" 
    android:background="@drawable/backgroundbutton" 
    android:layout_marginTop="30dp" /> 

,你可以看到我給的線性佈局的「總和」,所有他的孩子們給出的重量。 是否清楚?你需要什麼?