如何在Android中设置HorizontalScrollView?
我是 android 新手,但我已经完成了记事本教程。现在我正在尝试编写自己的布局。最终的布局在一页上将如下所示:
第一行: [ ------ 搜索栏 ----- ] [ 按钮 ]
第二行: [ text-tab ] [ text-tab ] [ text-tab ] <-- 单击其中之一以更改第三行内容
第三行:[填充其余高度的内容]
...当用户向左或向右滚动页面时,会显示另一个布局/页面。
因此,我开始使用 Eclipse 的图形编辑器来创建新的 Android XML 布局文件。 我将一个文本框拖到画布上,然后增加宽度,如上图所示。 然后我在它的末尾添加了按钮,这样第一行就完成了。
现在,当我尝试在下面添加任何内容时,它不起作用。所以我切换到 XML 视图。我复制并粘贴了 LinearLayout,以便可以对其进行编辑以制作第二行。
现在我收到错误: HorizontalScrollView 只能托管一个直接子级
好吧,所以我明白水平滚动视图应该只包含 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>
I'm new to android but I've done the notepad tutorial. Now I'm trying to write my own layout. The eventual layout will be like this on one page:
1st row: [ ------search bar----- ] [ button ]
2nd row: [ text-tab ] [ text-tab ] [ text-tab ] <-- click on one of these to change 3rd row content
3rd row: [ content to fill rest of height ]
... and when the user scrolls the page left or right another layout/page is shown.
So I started off using Eclipse's graphical editor for a new android XML layout file.
I dragged a text-box onto the canvas, then I increased the width as in the diagram above.
Then I added the button at the end of it, so that the first row was complete.
Now when I try to add anything below it doesn't work. So I switched to XML view. I copied and pasted the LinearLayout so that I could edit it to make the 2nd row.
Now I get the error: HorizontalScrollView can host only one direct child
Okay, so I understand the Horizontal Scroll View should only contain 1 LinearLayout but then what is the correct structure to set up this layout?
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)