【问题标题】:How to set up a HorizontalScrollView in Android?如何在 Android 中设置 Horizo​​ntalScrollView?
【发布时间】:2011-12-03 18:30:55
【问题描述】:

我是 android 新手,但我已经完成了记事本教程。现在我正在尝试编写自己的布局。最终的布局在一页上是这样的:

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

第 2 行:[ text-tab ] [ text-tab ] [ text-tab ]

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

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

所以我开始使用 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>

【问题讨论】:

    标签: android android-linearlayout horizontalscrollview


    【解决方案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>
    

    【讨论】:

    • 哦,所以您只需将整个东西包裹在另一个 LinearLayout 下...谢谢!
    • 在将这两个 LinearLayouts 包裹在一个垂直方向的布局下之后,两个水平方向的布局一个接一个地出现在同一行上。如何让第二个出现在另一行?
    • 有一个错字,它说orientation='vertical'应该是android:orientation='vertical'。问题已解决,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多