方法
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="垂直移动" />
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中的内容网上移动,负的时候往下移动
总结一句话:正负,上下,左右;
- Android View中getViewTreeObserver().addOnGlobalLayoutListener() (转)
转自:Android View中getViewTreeObserver().addOnGlobalLayoutListener() 我们知道在oncreate中View.getWidth和View.g ...
- Android view中的requestLayout和invalidate方法
Android view中的requestLayout和invalidate方法 requestLayout:当view确定自身已经不再适合现有的区域时,该view本身调用这个方法要求parent v ...
- Android开发中UI相关的问题总结
UI设计和实现是Android开发中必不可少的部分,UI做不好的话,丑到爆,APP性能再好,估计也不会有多少人用吧,而且如果UI和业务代码逻辑中间没有处理好,也会很影响APP的性能的.稍微总结一下,开 ...
- Android View中的控件和监听方法...
PS:居然三天没写博客了...今天补上...东西虽多,但是都是一些基础...代码多了一些,有人可能会这样问,粘这么多代码有毛用..其实对于一个Android的初学者来说,一个完整的代码是最容易帮助理解 ...
- android view 中各函数的执行顺数
这个就好像是 activity 的生命周期一样,如果我们要使用自定义的 view,那么就很有必要了解一下 view 的那些能够被重写的函数的执行顺序.废话不多讲,以常用的5个函数为例子,见下文: pa ...
- Android开发中Eclispe相关问题及相应解决(持续更新)
1.Eclipse项目中的Android Private Libraries没有自动生成. 一般而言,在Android开发中,项目中引用到的jar包会放到项目目录中的libs中,引入库会放到Andro ...
- 字体在Android View中的输出 drawText
Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标. public static class FontMetrics { public flo ...
- Android 和iOS中 View的滚动
在最近的程序中用到了Android中的View的滚动,记录一下,待总结.
- Android XML中引用自定义内部类view的四个why
今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...
随机推荐
- 从顺序随机I/O原理来讨论MYSQL MRR NLJ BNL BKA
http://blog.itpub.net/7728585/viewspace-2129502/
- 【JVM】idea启动项目时候 添加jvm启动参数显示详细日志
-verbose:class
- IOS开发退出应用程序的代码
IOS 开发中.我知道的两个退出程序的方法: 1. exit(0); 2. if([[UIApplication sharedApplication] respondsToSelector:@sele ...
- [Javascript] Await a JavaScript Promise in an async Function with the await Operator
The await operator is used to wait for a promise to settle. It pauses the execution of an async func ...
- 判断浏览器是否支持flash
虽然flash已经过时了,如腾讯视频一样,有很多网站还来不及或者根本就改不了原来的架构,所以我们需要判断flash视频(默认chrome和Firefox)等浏览器会自动阻止flash加载 , 这与Sa ...
- js json 对象
JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 大括号保存对象 中括号保存数组 JSON 名称/值对 JSON 数据的书写格式是 ...
- python(24)- 面向对象进阶
面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个‘函数’供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板 ...
- 笔记11 export to excel
参考两篇博客:http://blog.csdn.net/zyming0815/article/details/5939104 http://blog.csdn.net/g710710/article/ ...
- kubernetes高级之创建只读文件系统以及只读asp.net core容器
系列目录 使用docker创建只读文件系统 容器化部署对应用的运维带来了极大的方便,同时也带来一些新的安全问题需要考虑.比如黑客入侵到容器内,对容器内的系统级别或者应用级别文件进行修改,会造成难以估量 ...
- Dash 使用
花了 160 买了这个软件,至少看一遍它的 user guide,钱不能白花. https://kapeli.com/guide/guide.html 设置全局快捷键 Preference -> ...