如何在Android中设置Horizo​​ntalScrollView?

时间:2011-12-03 18:30:56

标签: android android-linearlayout horizontalscrollview

我是Android的新手,但我已经完成了记事本教程。现在我正在尝试编写自己的布局。最终的布局将在一个页面上如下所示:

第1行:[------搜索栏-----] [按钮]

第2行:[text-tab] [text-tab] [text-tab]< - 点击其中一个更改第3行内容

第3行:[填充剩余高度的内容]

...当用户向左或向右滚动页面时,会显示另一个布局/页面。

所以我开始使用Eclipse的图形编辑器来获取新的Android XML布局文件。 我将一个文本框拖到画布上,然后我增加了宽度,如上图所示。 然后我在它的末尾添加了按钮,以便第一行完成。

现在,当我尝试添加任何内容时,它不起作用。所以我切换到了XML视图。我复制并粘贴了LinearLayout,以便我可以编辑它以形成第二行。

现在我收到错误:Horizo​​ntalScrollView只能托管一个直接子项

好的,所以我理解水平滚动视图应该只包含1个LinearLayout,但是设置这个布局的正确结构是什么?

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView 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="wrap_content">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="1130dp"
        android:layout_height="72dp">
        <requestFocus/>
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="152dp"
        android:layout_height="72dp"
        android:text="Button"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="1130dp"
        android:layout_height="72dp">
        <requestFocus/>
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="152dp"
        android:layout_height="72dp"
        android:text="Button"/>
</LinearLayout>

</HorizontalScrollView>

1 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView 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="wrap_content"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="1130dp"
        android:layout_height="72dp">
        <requestFocus/>
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="152dp"
        android:layout_height="72dp"
        android:text="Button"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/editText1"
        android:layout_width="1130dp"
        android:layout_height="72dp">
        <requestFocus/>
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="152dp"
        android:layout_height="72dp"
        android:text="Button"/>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>