2013-03-30 32 views
0

我有18個按鈕的屏幕,是否有可能使這組按鈕可滾動,以便用戶可以看到超出可視區域的按鈕。我試圖把這些按鈕放在ScrollView中,但得到了只有它們只能是滾動視圖的1個子項的異常。製作屏幕上的按鈕可滾動

目前的按鈕存在於RelativeLayout的像:

<Button 
    android:id="@+id/btnChapter2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnChapter1" 
    android:layout_below="@+id/btnChapter1" 
    android:text="Chapter 2" /> 

<Button 
    android:id="@+id/btnChapter3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnChapter2" 
    android:layout_below="@+id/btnChapter2" 
    android:text="Chapter 3" /> 

<Button 
    android:id="@+id/btnChapter4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnChapter3" 
    android:layout_below="@+id/btnChapter3" 
    android:text="Chapter 4" /> 

<Button 
    android:id="@+id/btnChapter5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnChapter4" 
    android:layout_below="@+id/btnChapter4" 
    android:text="Chapter 5" /> 

<Button 
    android:id="@+id/btnChapter6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/btnChapter5" 
    android:layout_below="@+id/btnChapter5" 
    android:text="Chapter 6" /> 

當前屏幕的按鈕9,17和18被擰起來看起來:

enter image description here

回答

1

那麼ScrollView可能會只有一個孩子。在這種情況下,將LinearLayout或RelativeLayout作爲ScrollView的子項。把你的按鈕放在LinearLayout或RelativeLayout中。這應該做到了。示例代碼:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 
</ScrollView>