当前位置:   article > 正文

Android的Scrollview滚动视图控件_android scrollview横向滑动

android scrollview横向滑动

1.使用时的注意点?

        1.不用再ScrollView内部使用自带滚动条的控件或布局

        2.ScrollView直接子View只能有一个

        3.ScrollView只支持竖直滑动 如果想要横向滑动 HorizontalScrollView

2.具体案例实现

        前台页面代码

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5. <Button
  6. android:id="@+id/and_di"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:layout_weight="1"
  10. android:text="滚动到底部" />
  11. <Button
  12. android:id="@+id/and_top"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="1"
  16. android:text="滚动到顶部" />
  17. </LinearLayout>
  18. <ScrollView
  19. android:id="@+id/sv"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent">
  22. <LinearLayout
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:orientation="vertical">
  26. <TextView
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="顶部"/>
  30. <TextView
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:text="文本2"/>
  34. <TextView
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:text="文本3"/>
  38. <TextView
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:text="文本4"/>
  42. <TextView
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:text="文本5"/>
  46. <TextView
  47. android:layout_width="match_parent"
  48. android:layout_height="wrap_content"
  49. android:text="文本6"/>
  50. <TextView
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content"
  53. android:text="文本7"/>
  54. <TextView
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:text="文本8"/>
  58. <TextView
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:text="文本9"/>
  62. <TextView
  63. android:layout_width="match_parent"
  64. android:layout_height="wrap_content"
  65. android:text="文本10"/>
  66. <TextView
  67. android:layout_width="match_parent"
  68. android:layout_height="wrap_content"
  69. android:text="文本1"/>
  70. <TextView
  71. android:layout_width="match_parent"
  72. android:layout_height="wrap_content"
  73. android:text="文本2"/>
  74. <TextView
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content"
  77. android:text="文本3"/>
  78. <TextView
  79. android:layout_width="match_parent"
  80. android:layout_height="wrap_content"
  81. android:text="文本4"/>
  82. <TextView
  83. android:layout_width="match_parent"
  84. android:layout_height="wrap_content"
  85. android:text="文本5"/>
  86. <TextView
  87. android:layout_width="match_parent"
  88. android:layout_height="wrap_content"
  89. android:text="文本6"/>
  90. <TextView
  91. android:layout_width="match_parent"
  92. android:layout_height="wrap_content"
  93. android:text="文本7"/>
  94. <TextView
  95. android:layout_width="match_parent"
  96. android:layout_height="wrap_content"
  97. android:text="文本8"/>
  98. <TextView
  99. android:layout_width="match_parent"
  100. android:layout_height="wrap_content"
  101. android:text="文本9"/>
  102. <TextView
  103. android:layout_width="match_parent"
  104. android:layout_height="wrap_content"
  105. android:text="文本10"/>
  106. <TextView
  107. android:layout_width="match_parent"
  108. android:layout_height="wrap_content"
  109. android:text="文本1"/>
  110. <TextView
  111. android:layout_width="match_parent"
  112. android:layout_height="wrap_content"
  113. android:text="文本2"/>
  114. <TextView
  115. android:layout_width="match_parent"
  116. android:layout_height="wrap_content"
  117. android:text="文本3"/>
  118. <TextView
  119. android:layout_width="match_parent"
  120. android:layout_height="wrap_content"
  121. android:text="文本4"/>
  122. <TextView
  123. android:layout_width="match_parent"
  124. android:layout_height="wrap_content"
  125. android:text="文本5"/>
  126. <TextView
  127. android:layout_width="match_parent"
  128. android:layout_height="wrap_content"
  129. android:text="文本6"/>
  130. <TextView
  131. android:layout_width="match_parent"
  132. android:layout_height="wrap_content"
  133. android:text="文本7"/>
  134. <TextView
  135. android:layout_width="match_parent"
  136. android:layout_height="wrap_content"
  137. android:text="文本8"/>
  138. <TextView
  139. android:layout_width="match_parent"
  140. android:layout_height="wrap_content"
  141. android:text="文本9"/>
  142. <TextView
  143. android:layout_width="match_parent"
  144. android:layout_height="wrap_content"
  145. android:text="文本10"/>
  146. <TextView
  147. android:layout_width="match_parent"
  148. android:layout_height="wrap_content"
  149. android:text="文本1"/>
  150. <TextView
  151. android:layout_width="match_parent"
  152. android:layout_height="wrap_content"
  153. android:text="文本2"/>
  154. <TextView
  155. android:layout_width="match_parent"
  156. android:layout_height="wrap_content"
  157. android:text="文本3"/>
  158. <TextView
  159. android:layout_width="match_parent"
  160. android:layout_height="wrap_content"
  161. android:text="文本4"/>
  162. <TextView
  163. android:layout_width="match_parent"
  164. android:layout_height="wrap_content"
  165. android:text="文本5"/>
  166. <TextView
  167. android:layout_width="match_parent"
  168. android:layout_height="wrap_content"
  169. android:text="文本6"/>
  170. <TextView
  171. android:layout_width="match_parent"
  172. android:layout_height="wrap_content"
  173. android:text="文本7"/>
  174. <TextView
  175. android:layout_width="match_parent"
  176. android:layout_height="wrap_content"
  177. android:text="文本8"/>
  178. <TextView
  179. android:layout_width="match_parent"
  180. android:layout_height="wrap_content"
  181. android:text="文本9"/>
  182. <TextView
  183. android:layout_width="match_parent"
  184. android:layout_height="wrap_content"
  185. android:text="文本10"/>
  186. <TextView
  187. android:layout_width="match_parent"
  188. android:layout_height="wrap_content"
  189. android:text="文本1"/>
  190. <TextView
  191. android:layout_width="match_parent"
  192. android:layout_height="wrap_content"
  193. android:text="文本2"/>
  194. <TextView
  195. android:layout_width="match_parent"
  196. android:layout_height="wrap_content"
  197. android:text="文本3"/>
  198. <TextView
  199. android:layout_width="match_parent"
  200. android:layout_height="wrap_content"
  201. android:text="文本4"/>
  202. <TextView
  203. android:layout_width="match_parent"
  204. android:layout_height="wrap_content"
  205. android:text="文本5"/>
  206. <TextView
  207. android:layout_width="match_parent"
  208. android:layout_height="wrap_content"
  209. android:text="文本6"/>
  210. <TextView
  211. android:layout_width="match_parent"
  212. android:layout_height="wrap_content"
  213. android:text="文本7"/>
  214. <TextView
  215. android:layout_width="match_parent"
  216. android:layout_height="wrap_content"
  217. android:text="文本8"/>
  218. <TextView
  219. android:layout_width="match_parent"
  220. android:layout_height="wrap_content"
  221. android:text="文本9"/>
  222. <TextView
  223. android:layout_width="match_parent"
  224. android:layout_height="wrap_content"
  225. android:text="底部"/>
  226. </LinearLayout>
  227. </ScrollView>

后台Java代码

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private ScrollView sv;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. Button and_top=findViewById(R.id.and_top);
  8. Button and_di=findViewById(R.id.and_di);
  9. sv=findViewById(R.id.sv);
  10. and_di.setOnClickListener(this);
  11. and_top.setOnClickListener(this);
  12. }
  13. @Override
  14. public void onClick(View view) {
  15. if (view.getId()==R.id.and_di){
  16.                 //滚动到底部
  17. sv.fullScroll(ScrollView.FOCUS_DOWN);
  18. }else if (view.getId()==R.id.and_top){
  19.                 //滚动到顶部
  20. sv.fullScroll(ScrollView.FOCUS_UP);
  21. }
  22. }
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/466552
推荐阅读
相关标签
  

闽ICP备14008679号