使屏幕按钮可滚动

时间:2013-03-30 16:58:32

标签: java android android-ui

我有18个按钮的屏幕,是否可以使这组按钮可滚动,以便用户可以看到超出可视区域的按钮。我尝试将这些按钮放在ScrollView中,但是只有它们只能是滚动视图的一个孩子。

目前,按钮存在于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 个答案:

答案 0 :(得分: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>
相关问题