Android学习-滚动视图ScrollView和HorizontalScrollView
程序员文章站
2022-04-13 22:31:36
一、简介: ScrollView,通过官方文档的继承关系可以看出,它继承自FrameLayout,所以它是一种特殊类型的FrameLayout,因为它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。值得注意的是,ScrollView只能包含一个子视图或视图组,在实际项目中,通常包含的是一个 ......
一、简介:
scrollview,通过官方文档的继承关系可以看出,它继承自framelayout,所以它是一种特殊类型的framelayout,因为它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。值得注意的是,scrollview只能包含一个子视图或视图组,在实际项目中,通常包含的是一个垂直的linearlayout。
二、scrollview代码块:
- 在activity_main.xml中添加一个超出页面范围的按钮:
<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="wrap_content" android:orientation="vertical" > <button android:id="@+id/btn_textview" android:layout_width="match_parent" android:layout_height="match_parent" android:text="textview" android:textallcaps="false"/> <button android:id="@+id/btn_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" android:textallcaps="false"/> <button android:id="@+id/btn_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="edittext" android:textallcaps="false"/> <button android:id="@+id/btn_radiobutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="radiobutton" android:textallcaps="false"/> <button android:id="@+id/btn_checkbox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="checkbox" android:textallcaps="false"/> <button android:id="@+id/btn_imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="imageview" android:textallcaps="false"/> <button android:id="@+id/btn_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="listview" android:textallcaps="false"/> <button android:id="@+id/btn_gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="gridview" android:textallcaps="false"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test" android:textallcaps="false" android:layout_margintop="300dp"/> </linearlayout>
- 更改为scrollview布局(scrollview下子元素只能有一个):
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <button android:id="@+id/btn_textview" android:layout_width="match_parent" android:layout_height="match_parent" android:text="textview" android:textallcaps="false"/> <button android:id="@+id/btn_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" android:textallcaps="false"/> <button android:id="@+id/btn_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="edittext" android:textallcaps="false"/> <button android:id="@+id/btn_radiobutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="radiobutton" android:textallcaps="false"/> <button android:id="@+id/btn_checkbox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="checkbox" android:textallcaps="false"/> <button android:id="@+id/btn_imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="imageview" android:textallcaps="false"/> <button android:id="@+id/btn_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="listview" android:textallcaps="false"/> <button android:id="@+id/btn_gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="gridview" android:textallcaps="false"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test" android:textallcaps="false" android:layout_margintop="300dp"/> </linearlayout> </scrollview>
- 运行截图:
三、horizontalscrollview代码块:
- 在原有scrollview基础上写入horizontalscrollview(horizontalscrollview下子元素只能有一个):
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <button android:id="@+id/btn_textview" android:layout_width="match_parent" android:layout_height="match_parent" android:text="textview" android:textallcaps="false"/> <button android:id="@+id/btn_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" android:textallcaps="false"/> <button android:id="@+id/btn_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="edittext" android:textallcaps="false"/> <button android:id="@+id/btn_radiobutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="radiobutton" android:textallcaps="false"/> <button android:id="@+id/btn_checkbox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="checkbox" android:textallcaps="false"/> <button android:id="@+id/btn_imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="imageview" android:textallcaps="false"/> <button android:id="@+id/btn_listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="listview" android:textallcaps="false"/> <button android:id="@+id/btn_gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="gridview" android:textallcaps="false"/> <horizontalscrollview android:layout_width="wrap_content" android:layout_height="wrap_content"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <button android:layout_width="300dp" android:layout_height="300dp" android:text="test" android:textallcaps="false"/> <button android:layout_width="300dp" android:layout_height="300dp" android:text="test" android:textallcaps="false"/> <button android:layout_width="300dp" android:layout_height="300dp" android:text="test" android:textallcaps="false"/> <button android:layout_width="300dp" android:layout_height="300dp" android:text="test" android:textallcaps="false"/> </linearlayout> </horizontalscrollview> </linearlayout> </scrollview>
- 运行截图(既能上下滚动也能左右滚动):
上一篇: 各种脸型适合的发型分别是什么
下一篇: 城管来了
推荐阅读
-
[Android] Android最简单ScrollView和ListView滚动冲突解决方案
-
Android学习-滚动视图ScrollView和HorizontalScrollView
-
Android中通过Java代码实现ScrollView滚动视图-以歌词滚动为例
-
Android学习笔记十:基本视图组件:ImageView和ImageButton
-
Android学习笔记七:基本视图组件:RadioGroup和RadioButton
-
Android学习笔记17:中级视图组件DatePicker和TimePicker
-
Android学习-滚动视图ScrollView和HorizontalScrollView
-
Android中通过Java代码实现ScrollView滚动视图-以歌词滚动为例
-
[Android] Android最简单ScrollView和ListView滚动冲突解决方案
-
Android学习笔记十:基本视图组件:ImageView和ImageButton