Android中使用HorizontalScrollView横向滑动布局
2015-12-05 13:52
417 查看
在Android中,ListView通常用来实现纵向滚动的列表,而HorizontalScrollView则可以实现横向滚动的列表项。
引入HorizontalScrollView控件很简单,例如在一个LinearLayout中:
这时我们没有添加任何子控件,因此IDE提示我们“This HorizontalScrollView view is useless (no children, no background, no id, no style)”
作为示例,我们尝试在这个HorizontalScrollView中添加若干个按钮:
这时IDE会提示我们:“A scroll view can have only one child”。
原来,在HorizontalScrollView中,只可存在一个直接的子View。因此正确的用法是使用一个如LinearLayout这样的控件,将多个子View放入其中,改好的布局文件是这样的:
注意HorizontalScrollView中LinearLayout的witdh属性应为”wrap_content”。
现在运行APP,便可以通过横向滑动,将多余的按钮显示出来了。
引入HorizontalScrollView控件很简单,例如在一个LinearLayout中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> </HorizontalScrollView> </LinearLayout>
这时我们没有添加任何子控件,因此IDE提示我们“This HorizontalScrollView view is useless (no children, no background, no id, no style)”
作为示例,我们尝试在这个HorizontalScrollView中添加若干个按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.viewholdertest.MainActivity" > <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello"/> ......(余下的省略) </HorizontalScrollView> </LinearLayout>
这时IDE会提示我们:“A scroll view can have only one child”。
原来,在HorizontalScrollView中,只可存在一个直接的子View。因此正确的用法是使用一个如LinearLayout这样的控件,将多个子View放入其中,改好的布局文件是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.viewholdertest.MainActivity" > <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" /> ......(余下的省略) </LinearLayout> </HorizontalScrollView> </LinearLayout>
注意HorizontalScrollView中LinearLayout的witdh属性应为”wrap_content”。
现在运行APP,便可以通过横向滑动,将多余的按钮显示出来了。
相关文章推荐
- Android之如何使用junit
- android camera系统3A模式及其状态转换(二)
- android camera系统3A模式及其状态转换(一)
- android .9文件的一点处理
- Android动画大总结
- android项目apk打包及反编译
- 上百个Android开源项目分享
- Android 中的 Service 三种启动方式
- android camera HAL v3.0中元数据及其控制
- android项目双击或者多击的实现
- android 蓝牙4.2.2分析研究
- Android 中的 Service 全面总结
- Android Service与Activity之间通信的几种方式
- ListView异步加载图片,完美实现图文混排
- Android群英传-拼图游戏puzzle-代码设计和实现
- Android群英传-拼图游戏puzzle-代码设计和实现
- Android群英传-拼图游戏puzzle-代码设计和实现
- android在单身的对象和一些数据的问题被释放
- Android判断Service是否运行
- Android-系统分享-1