方法 scrollTo: (内容的左上角)达到某个地点 scrollBy: 根据当前位置,再移动多少 属性: mScrollX, 以下是文档解释 The offset, in pixels, by which the content of this view is scrolled horizontally. mScrollY, 以下是文档解释 The offset, in pixels, by which the content of this view is scrolled vertically. 网上有很多blog翻译,就是简单的说是偏移量,但是没有具体的说相对哪个坐标的偏移量或者是哪个点的偏移量; 我的mScrollX/mScrollY的理解是: 当View水平滚动完成之后,此View内容的左边相对于View容器本身左边的偏移量;同理竖直方向也是; 当View竖直滚动完成之后,此View内容的上边相对于View容器本身上边的偏移量; 所有的滚动都是针对于View 里面的内容; 以下是例子 布局代码: 001<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 002xmlns:tools="http://schemas.android.com/tools" 003android:layout_width="fill_parent" 004android:orientation="vertical" 005android:layout_height="fill_parent" > 006 007<HorizontalScrollView 008 android:background="#f888" 009 android:id="@+id/sv" 010 android:layout_width="fill_parent" 011 android:layout_height="wrap_content" > 012 013 <LinearLayout 014 android:id="@+id/ll" 015 android:layout_width="wrap_content" 016 android:layout_height="fill_parent" 017 android:orientation="horizontal" 018 android:paddingBottom="@dimen/activity_vertical_margin" 019 android:paddingLeft="@dimen/activity_horizontal_margin" 020 android:paddingRight="@dimen/activity_horizontal_margin" 021 android:paddingTop="@dimen/activity_vertical_margin" > 022 023 <Button 024 android:layout_width="300dp" 025 android:onClick="scrollX" 026 android:layout_height="wrap_content" 027 android:background="#ff00" 028 android:text="水平移动" /> 029 <Button 030 android:onClick="scrollX" 031 android:layout_width="300dp" 032 android:layout_height="wrap_content" 033 android:background="#f0f0" 034 android:text="水平移动" /> 035 <Button 036 android:onClick="scrollX" 037 android:layout_width="300dp" 038 android:layout_height="wrap_content" 039 android:background="#f00f" 040 android:text="水平移动" /> 041 <Button 042 android:onClick="scrollX" 043 android:layout_width="300dp" 044 android:layout_height="wrap_content" 045 android:background="#ff0f" 046 android:text="水平移动" /> 047 <Button 048 android:onClick="scrollX" 049 android:layout_width="300dp" 050 android:layout_height="wrap_content" 051 android:background="#fff0" 052 android:text="水平移动" /> 053 <Button 054 android:onClick="scrollX" 055 android:layout_width="300dp" 056 android:layout_height="wrap_content" 057 android:background="#f0ff" 058 android:text="水平移动" /> 059 </LinearLayout> 060</HorizontalScrollView> 061 062<ScrollView 063 android:id="@+id/sv2" 064 android:layout_width="fill_parent" 065 android:background="#fccc" 066 android:layout_height="wrap_content" 067 android:layout_weight="1" > 068 069 <LinearLayout 070 android:layout_width="match_parent" 071 android:layout_height="3600px" 072 android:orientation="vertical" 073 android:paddingLeft="@dimen/activity_horizontal_margin" 074 android:paddingRight="@dimen/activity_horizontal_margin" 075 android:paddingBottom="1200px" 076 tools:context=".MainActivity" > 077 <Button 078 android:background="#ff00" 079 android:layout_width="wrap_content" 080 android:layout_height="300px" 081 android:onClick="scrollY" 082 android:text="垂直移动" /> 083 <Button 084 android:background="#f0f0" 085 android:layout_width="wrap_content" 086 android:layout_height="300px" 087 android:onClick="scrollY" 088 android:text="垂直移动" /> 089 <Button 090 android:background="#f00f" 091 android:layout_width="wrap_content" 092 android:layout_height="300px" 093 android:onClick="scrollY" 094 android:text="垂直移动" /> 095 <Button 096 android:background="#f0ff" 097 android:layout_width="wrap_content" 098 android:layout_height="300px" 099 android:onClick="scrollY" 100 android:text="垂直移动" /> 101 <Button 网页特效代码 102 android:background="#ff0f" 103 android:layout_width="wrap_content" 104 android:layout_height="300px" 105 android:onClick="scrollY" 106 android:text="垂直移动" /> 107 <Button 108 android:background="#fff0" 109 android:layout_width="wrap_content" 110 android:layout_height="300px" 111 android:onClick="scrollY" 112 android:text="垂直移动" /> 113 </LinearLayout> 114</ScrollView> 主要Activity代码 01public void scrollY(View view){ 02 /** 03 * 当scrollBy的Y为负:ScrollView中的内容向下移动 04 * 当scrollBy的Y为正:....................................... 上移动 05 */http://www.huiyi8.com/jiaoben/ 06 svY.scrollBy(0, 300); // 内容向上走 07 System.out.println("YY: "+svY.getScrollY()); 08} 09 10public void scrollX(View view){ 11 /** 12 * X为负: 右移动 13 * X为正:左移动 14 */ 15 svX.scrollBy(300, 0);// 内容向左走 16 System.out.println("XX: "+svX.getScrollX()); 17} 效果图 水平滚动图

竖直滚动图

从效果和图片来看总结 scrollBy(x,y )

  • 当x为正的时候,view中的内容往左边移动,负的时候往右边移动
  • 但y为正的时候,view中的内容网上移动,负的时候往下移动

总结一句话:正负,上下,左右;

更多相关文章

  1. "Copy" did not complete normally. Please see the log for mor
  2. Android(安卓)Q Beta 更新内容介绍 [转载]
  3. 关于Android(安卓)Studio 查看源码 throw new RuntimeException(
  4. Android(安卓)draw、onDraw、dispatchDraw、invalidate、compute
  5. Android内容提供器-读取通讯录
  6. Android(安卓)studio gradle 配置阿里镜像
  7. Android(安卓)简单记事本
  8. android 修改标题样式以及内容
  9. Android的Hello World

随机推荐

  1. 【Android开发--新手必看篇】ProgressBar
  2. Android(安卓)菜单开发和视图处理
  3. Android自定义Dialog实现通用圆角对话框
  4. Android(安卓)Activity之间切换的各种效
  5. Android读取XML文件中的数据
  6. Activity全面解析
  7. Android(安卓)获取view在屏幕中的位置
  8. Android之路之十六(重要組件之BroadcastRe
  9. Android(安卓)Studio使用技巧系列教程(三)
  10. Androd studio无线调试及镜像投屏