经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android学习-滚动视图ScrollView和HorizontalScrollView
来源:cnblogs  作者:YouChaoMin  时间:2018/11/29 9:19:32  对本文有异议

一、简介:

ScrollView,通过官方文档的继承关系可以看出,它继承自FrameLayout,所以它是一种特殊类型的FrameLayout,因为它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。值得注意的是,ScrollView只能包含一个子视图或视图组,在实际项目中,通常包含的是一个垂直的LinearLayout。

二、ScrollView代码块:

  • 在activity_main.xml中添加一个超出页面范围的按钮:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. >
  7. <Button
  8. android:id="@+id/btn_textview"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:text="TextView"
  12. android:textAllCaps="false"/>
  13. <Button
  14. android:id="@+id/btn_button"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:text="BUTTON"
  18. android:textAllCaps="false"/>
  19. <Button
  20. android:id="@+id/btn_edittext"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:text="EditText"
  24. android:textAllCaps="false"/>
  25. <Button
  26. android:id="@+id/btn_radiobutton"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="RadioButton"
  30. android:textAllCaps="false"/>
  31. <Button
  32. android:id="@+id/btn_checkbox"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:text="CheckBox"
  36. android:textAllCaps="false"/>
  37. <Button
  38. android:id="@+id/btn_imageview"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:text="ImageView"
  42. android:textAllCaps="false"/>
  43. <Button
  44. android:id="@+id/btn_listview"
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:text="ListView"
  48. android:textAllCaps="false"/>
  49. <Button
  50. android:id="@+id/btn_gridview"
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content"
  53. android:text="GridView"
  54. android:textAllCaps="false"/>
  55. <Button
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content"
  58. android:text="test"
  59. android:textAllCaps="false"
  60. android:layout_marginTop="300dp"/>
  61. </LinearLayout>
  • 更改为ScrollView布局(ScrollView下子元素只能有一个):
  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical">
  10. <Button
  11. android:id="@+id/btn_textview"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:text="TextView"
  15. android:textAllCaps="false"/>
  16. <Button
  17. android:id="@+id/btn_button"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:text="BUTTON"
  21. android:textAllCaps="false"/>
  22. <Button
  23. android:id="@+id/btn_edittext"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:text="EditText"
  27. android:textAllCaps="false"/>
  28. <Button
  29. android:id="@+id/btn_radiobutton"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="RadioButton"
  33. android:textAllCaps="false"/>
  34. <Button
  35. android:id="@+id/btn_checkbox"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:text="CheckBox"
  39. android:textAllCaps="false"/>
  40. <Button
  41. android:id="@+id/btn_imageview"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. android:text="ImageView"
  45. android:textAllCaps="false"/>
  46. <Button
  47. android:id="@+id/btn_listview"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content"
  50. android:text="ListView"
  51. android:textAllCaps="false"/>
  52. <Button
  53. android:id="@+id/btn_gridview"
  54. android:layout_width="match_parent"
  55. android:layout_height="wrap_content"
  56. android:text="GridView"
  57. android:textAllCaps="false"/>
  58. <Button
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:text="test"
  62. android:textAllCaps="false"
  63. android:layout_marginTop="300dp"/>
  64. </LinearLayout>
  65. </ScrollView>
  • 运行截图:
    ScrollView

三、HorizontalScrollView代码块:

  • 在原有ScrollView基础上写入HorizontalScrollView(HorizontalScrollView下子元素只能有一个):
  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical">
  10. <Button
  11. android:id="@+id/btn_textview"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:text="TextView"
  15. android:textAllCaps="false"/>
  16. <Button
  17. android:id="@+id/btn_button"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:text="BUTTON"
  21. android:textAllCaps="false"/>
  22. <Button
  23. android:id="@+id/btn_edittext"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:text="EditText"
  27. android:textAllCaps="false"/>
  28. <Button
  29. android:id="@+id/btn_radiobutton"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="RadioButton"
  33. android:textAllCaps="false"/>
  34. <Button
  35. android:id="@+id/btn_checkbox"
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:text="CheckBox"
  39. android:textAllCaps="false"/>
  40. <Button
  41. android:id="@+id/btn_imageview"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. android:text="ImageView"
  45. android:textAllCaps="false"/>
  46. <Button
  47. android:id="@+id/btn_listview"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content"
  50. android:text="ListView"
  51. android:textAllCaps="false"/>
  52. <Button
  53. android:id="@+id/btn_gridview"
  54. android:layout_width="match_parent"
  55. android:layout_height="wrap_content"
  56. android:text="GridView"
  57. android:textAllCaps="false"/>
  58. <HorizontalScrollView
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content">
  61. <LinearLayout
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:orientation="horizontal">
  65. <Button
  66. android:layout_width="300dp"
  67. android:layout_height="300dp"
  68. android:text="Test"
  69. android:textAllCaps="false"/>
  70. <Button
  71. android:layout_width="300dp"
  72. android:layout_height="300dp"
  73. android:text="Test"
  74. android:textAllCaps="false"/>
  75. <Button
  76. android:layout_width="300dp"
  77. android:layout_height="300dp"
  78. android:text="Test"
  79. android:textAllCaps="false"/>
  80. <Button
  81. android:layout_width="300dp"
  82. android:layout_height="300dp"
  83. android:text="Test"
  84. android:textAllCaps="false"/>
  85. </LinearLayout>
  86. </HorizontalScrollView>
  87. </LinearLayout>
  88. </ScrollView>
  • 运行截图(既能上下滚动也能左右滚动):
    ScrollView
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号