Android的ScrollView和HorizontalScrollView

蔚落 2023-07-14 08:20 43阅读 0赞

理论部分:

https://blog.csdn.net/scyatcs/article/details/7942585

1、ScrollView和HorizontalScrollView是为控件或者布局添加滚动条

2、上述两个控件只能有一个孩子,但是它并不是传统意义上的容器

3、上述两个控件可以互相嵌套

4、滚动条的位置现在的实验结果是:可以由layout_width和layout_height设定

5、ScrollView用于设置垂直滚动条,HorizontalScrollView用于设置水平滚动条:需要注意的是,有一个属性是 scrollbars 可以设置滚动条的方向:但是ScrollView设置成horizontal是和设置成none是效果同,HorizontalScrollView设置成vertical和none的效果同。


实践部分:

目录

1.垂直滚动:Scroll

2.水平滚动:HorizontalScrollView

ScrollView称为滚动视图,是当在一个屏幕的像素显示不下的时候,可以采用滑动的方式,显示在UI上


1.垂直滚动:ScrollView

新建一个应用程序:

在MainActivity的布局文件上做个实验,现在设置了按钮1和按钮2后还剩下一些空位:

70

再设置一个按钮3让他超出屏幕之外:

70 1

现在去运行程序,是滑动不了, 看不到按钮3的。

70 2

应该如何设置呢?


1.改变这个布局文件的根布局:把根布局改成:ScrollView

注意:ScrollView的子元素只能有一个,所以得增加一个LinearLayout布局,把其他按键放在这个LinearLayout中,那么ScrollViewd的子元素就只有一个LinearLayout了,而LinearLayout的子元素不限制。

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:padding="10dp">
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:orientation="vertical"
  10. >
  11. <Button
  12. android:id="@+id/IVButton_Id"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="跳转到ImageView"
  16. android:textSize="20dp"
  17. android:textAllCaps="false"/>
  18. <Button
  19. android:id="@+id/LVButton_Id"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:text="跳转到ListView"
  23. android:textSize="20dp"
  24. android:textAllCaps="false"/>
  25. <Button
  26. android:id="@+id/GVButton_Id"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="跳转到GridView"
  30. android:textSize="20dp"
  31. android:textAllCaps="false"/>
  32. <Button
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:text="按钮1"
  36. android:textSize="20dp"
  37. android:textAllCaps="false"
  38. android:layout_marginTop="100dp"/>
  39. <Button
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:text="按钮2"
  43. android:textSize="20dp"
  44. android:textAllCaps="false"
  45. android:layout_marginTop="100dp"/>
  46. <Button
  47. android:layout_width="match_parent"
  48. android:layout_height="wrap_content"
  49. android:text="按钮3"
  50. android:textSize="20dp"
  51. android:textAllCaps="false"
  52. android:layout_marginTop="300dp"/>
  53. </LinearLayout>
  54. </ScrollView>

运行程序,现在就可以向下滚动,看到按钮3了:

70 3


2.水平滚动:HorizontalScrollView

在LinearLayout里新建一个HorizontalScrollView,同样他的子元素只能有一个

70 4

所以在HorizontalScrollView布局中再加一个子布局LinearLayout,且LinearLayout为水平方向:

70 5

70 6

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:padding="10dp">
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:orientation="vertical"
  10. >
  11. <Button
  12. android:id="@+id/IVButton_Id"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="跳转到ImageView"
  16. android:textSize="20dp"
  17. android:textAllCaps="false"/>
  18. <Button
  19. android:id="@+id/LVButton_Id"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:text="跳转到ListView"
  23. android:textSize="20dp"
  24. android:textAllCaps="false"/>
  25. <Button
  26. android:id="@+id/GVButton_Id"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="跳转到GridView"
  30. android:textSize="20dp"
  31. android:textAllCaps="false"/>
  32. <Button
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:text="按钮1"
  36. android:textSize="20dp"
  37. android:textAllCaps="false"
  38. android:layout_marginTop="100dp"/>
  39. <Button
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:text="按钮2"
  43. android:textSize="20dp"
  44. android:textAllCaps="false"
  45. android:layout_marginTop="160dp"/>
  46. <HorizontalScrollView
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content">
  49. <LinearLayout
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:orientation="horizontal">
  53. <Button
  54. android:layout_width="200dp"
  55. android:layout_height="300dp"
  56. android:text="按钮3" />
  57. <Button
  58. android:layout_width="200dp"
  59. android:layout_height="300dp"
  60. android:text="按钮4" />
  61. </LinearLayout>
  62. </HorizontalScrollView>
  63. </LinearLayout>
  64. </ScrollView>

运行应用程序,因为外面还嵌套了一层ScrollView所以能垂直滚动和水平滚动:

70 7


发表评论

表情:
评论列表 (有 0 条评论,43人围观)

还没有评论,来说两句吧...

相关阅读